python小实例:购物车

功能:

输入工资,选择商品,按q退出,输入数字购买商品,余额够的话可以一直购买,否则退出

 

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 
 5 product_list=[('矿泉水',100),('冰红茶',110),('奶茶',90)]
 6 shop_list=[]
 7 salary=input("请输入您的工资:")
 8 #isdigit() 判断是否是数字
 9 if salary.isdigit():
10     salary=int(salary)
11     while True:
12          #enumerate 用于遍历序列中的元素以及它们的下标
13          for k,v in enumerate(product_list):
14 
15              line=':'.join([v[0],str(v[1])])
16              line='.'.join([str(k+1),line])
17              print(line)
18 
19          chose=input("请输入您选择的商品:")
20          if chose=='q':
21               print('您已经购买了:')
22               for line in shop_list:
23                    print(line)
24               print("您剩余金额:{salary}".format(salary=salary))
25               break
26          elif chose.isdigit():
27              chose= int(chose)
28              if chose>len(product_list) or chose<1:
29                  print("输入有误")
30                  continue
31 
32              chose= chose-1
33              cost=product_list[chose][1]
34 
35              if salary<cost:
36                  print("您的余额不足")
37                  continue
38              salary=salary-cost
39              shop_list.append(product_list[chose])
40              print("您已经购买 {name},余额{salary}".format(name=product_list[chose][0],salary=salary))
41 
42 else:
43     print('输入有误!')

 

转载于:https://www.cnblogs.com/alstonlee/p/6393521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值