小阳儿儿

python-购物车
 1 #购物车
 2 #商品信息
 3 product_list=[
 4     ('mac',10000),
 5     ('car',10000),
 6     ('book',50),
 7     ('shoes',200),
 8     ('kindle',800),
 9     ('food',10),
10 ]
11 shopping_car=[]
12 saving=input("please input your money:")
13 if saving.isdigit():
14     saving=int(saving)
15 else:
16     print("invalid input")
17 while True:
18     for i ,v in enumerate(product_list,1):
19         print(i,"<<<",v)
20     choice=input("please input the number of these "
21                      "goods which you want to buy(press q to eixt)")
22     if choice.isdigit():
23         choice=int(choice)
24         if choice>0 and choice < len(product_list):
25             p_iterm=product_list[choice-1]
26             if saving >=p_iterm[1]:
27                 saving-=p_iterm[1]
28                 shopping_car.append(p_iterm)
29             else:
30                 print("don't have ennough money")
31             print(p_iterm)
32         else:
33             print("the number is not exit")
34     elif choice=='q':
35         print("---------you have buy somethings as follows------- ")
36         for i in shopping_car:
37             print(i)
38         print("the rest of money%s"%saving)
39         break
40     else:
41         print("invalid input")

 

posted on 2019-01-06 15:54  小阳儿儿  阅读(73)  评论(0编辑  收藏  举报