用python编写购物程序
2021-05-23 13:32
标签:lis 代码 pen 推出 for [] code auth \n 要求: 代码: 用python编写购物程序 标签:lis 代码 pen 推出 for [] code auth \n 原文地址:https://www.cnblogs.com/BIT-taozhen/p/9734742.html
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author:James Tao
4
5
6 salary=int(input(‘请输入您的工资:‘))
7 list_of_goods=[[‘iphone‘,5800],[‘Mac Pro‘,12000],[‘Starbuck‘,31],[‘Bicycle‘,800]]
8 balance=salary
9 goods_of_bought=[]
10 goods_of_categorical={}
11
12 judge=True
13 while balance>0 and judge:
14
15 #打印出商品列表及编号
16 for i in range(len(list_of_goods)):
17 print(‘支持购买的商品有:{goods},对应编号为:{n} \n‘.format(goods=list_of_goods[i][0],n=i))
18
19 number = int(input(‘请输入您要购买的商品编号:‘))
20 #计算余额
21 balance=balance-int(list_of_goods[number][1])
22
23 #判断余额是否为0
24 if balance>0:
25
26 #将购买的商品加入购物车
27 goods_of_bought.append(list_of_goods[number][0])
28 quit1=input(‘继续购买?(Y?N):‘)
29 if quit1==‘N‘:
30 judge=False
31
32 else:
33
34 #若余额小于0,将上一次购买的商品金额去除
35 balance = balance + int(list_of_goods[number][1])
36 quit2=input(‘余额不足,是否退出?(Y/N):‘)
37 if quit2==‘Y‘:
38 judge=False
39
40 #判断是否购买了商品
41 if goods_of_bought: #如果列表为空等于False
42
43 #统计购买的商品种类
44 goods_of_set=set(goods_of_bought)
45
46 #统计购买的商品数量并输出
47 for item in goods_of_set:
48 goods_of_categorical[item]=goods_of_bought.count(item)
49 print(‘您购买的商品及数量为:‘,goods_of_categorical)
50
51 else:
52 print(‘您未购买任何商品‘)
53
54 print(‘余额为:‘,balance)
下一篇:JAVA知识点总结篇(二)