码迷,mamicode.com
首页 > 编程语言 > 详细

python 商品购物车

时间:2018-02-28 10:37:49      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:car   exit   app   商品   enumerate   选择   lease   put   pos   

 1 Shopping_Cart=[]
 2 
 3 commodity=[("iphone",5000),
 4 ("bike",200),
 5 ("book",100),
 6 ("computer",3000),
 7 ("car",10000),
 8 ]
 9 salary=input("please input your salary:")
10 
11 # print(commodity)
12 if salary.isdigit():
13     salary=int(salary)
14     while True:
15         for index,item in enumerate(commodity):
16             print(index,item)
17         Use_choice = input("please choose you want to buy commodity num:")
18         if Use_choice.isdigit():
19             Use_choice=int(Use_choice)
20             if Use_choice<len(commodity) and Use_choice>=0 :
21                 p_item=commodity[Use_choice]
22                 # print(p_item[1])
23                 if p_item[1]<= salary:
24                     Shopping_Cart.append(p_item)
25                     salary-=p_item[1]
26                     print("Shopping_Cart added %s ,your current balance is \033[31;1m %s \033[0m" % (p_item,salary))
27                 else :
28                     print("\033[41;1m 余额不足,请重新选择 %s \033[0m" %salary)
29             else:
30                 print("please iput correct product code \033[31;1m %s \033[0m" % Use_choice)
31         elif Use_choice=="q" :
32             print("--------shopping list---------")
33             for i in Shopping_Cart:
34                 print(i)
35             exit("成功退出购买商品")
36         else:
37             print("please input Correct commodinty num \033[31;1m %s \033[0m"%Use_choice)
38 else:
39     print("\033[31;1mplease input Correct salary %s \033[0m"%salary)

 

python 商品购物车

标签:car   exit   app   商品   enumerate   选择   lease   put   pos   

原文地址:https://www.cnblogs.com/JIM-FAN/p/8481748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!