购物车练习

Posted gkx0731

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车练习相关的知识,希望对你有一定的参考价值。

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 33[31;1m%s33[0m." %salary)
26                 else:
27                     print("you have money only 33[31;1m%s33[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

 

以上是关于购物车练习的主要内容,如果未能解决你的问题,请参考以下文章

vue.js实战——购物车练习(包含全选功能)

angularjs购物车练习

列表练习题 简单购物车

购物车练习

Python 练习题_简易购物车

Python初学时购物车程序练习实例