码迷,mamicode.com
首页 > 其他好文 > 详细

购物车练习

时间:2018-08-04 20:21:51      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:ase   gif   selection   and   size   cycle   sel   option   print   

python基础之购物车练习:

1.输入月薪打印商品列表,

2.选购商品,显示余额,

3.可随时退出,并打印所购商品及余额。

技术分享图片
 1 product_list = [
 2     ("iphone",5000),
 3     ("MacPro",10000),
 4     ("Book",120),
 5     ("bicycle",1000),
 6     ("TV",2000),
 7     ("washer",3000),
 8     ("pen",50)
 9 ]
10 shopping_list = []
11 salary = input("Input your salary : ")
12 if salary.isdigit():
13     salary = int(salary)
14     while True:
15         for index,item in enumerate(product_list):
16             print(index,item)
17         user_choice = input("what do you want to buy,or enter q to leave: ")
18         if user_choice.isdigit():
19             user_choice = int(user_choice)
20             if user_choice >= 0 and user_choice < len(product_list):
21                 p_item = product_list[user_choice]
22                 if salary >= p_item[1]:
23                     shopping_list.append(p_item[0])
24                     salary = salary-p_item[1]
25                     print("add your choice into shoppingcart,your current balance is \033[31;1m%s\033[0m." %salary)
26                 else:
27                     print("you have money only \033[31;1m%s\033[0m,can‘t payment"%salary)
28             else:
29                 print("your choice is outof selection")
30         elif user_choice == "q":
31             print("-----shopping cart-----")
32             for p in shopping_list:
33                 print(p)
34             exit()
35         else:
36             print("invaild option")
37 else:
38     print("Please input the digit")
View Code

 

购物车练习

标签:ase   gif   selection   and   size   cycle   sel   option   print   

原文地址:https://www.cnblogs.com/gkx0731/p/9419545.html

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