Python学习第二天——购物车程序

2018-06-18 02:25:10来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
  5. 如余额不足,可充值

 

 1 #coding=utf-8
 2 #Version:python 3.6.0
 3 #Tools:Pycharm 2017.3.2
 4 _date_ = '2018/4/16/016 14:50'
 5 _author_ = 'Hongyong'
 6 
 7 salary = int(input("Please input your salary: "))
 8 shopping_mart = []
 9 items = (["Huawei","",2800],
10          ["Earphone","",300],
11          ["Book","",80])
12 msg_items = '''
13 ----------items----------
14 1. Huawei     ¥  2800
15 2. Earphone   ¥  300
16 3. Book       ¥  80
17 -------------------------
18 '''
19 while True:
20     print(msg_items)
21     user_choice = input("Please choose goods: ")
22     if user_choice.isdigit():
23         p_index = int(user_choice)
24         if p_index > 0 and p_index <= len(items):
25             if salary >= items[p_index - 1][2]:
26                 shopping_mart.append(items[p_index- 1])
27                 salary -= int(items[p_index - 1][2])
28                 print("You have bought {name} !".format(name=items[p_index - 1][1]))
29                 print("Your balance is: ¥", salary)
30             else:
31                 print("Your balance is not enough! Please try sth else.")
32                 recharge_ans = input("Do you want to recharge?")
33                 if recharge_ans == "y":
34                     recharge = int(input("Please input money: "))
35                     print("Please wait for a while...")
36                     salary += recharge
37                     print("You have recharged successfully!")
38                     print("And the balance is: ", salary, "now!")
39         else:
40             print("Invalid Choice!")
41             print("You are supposed to enter from 1 to",len(items))
42     elif user_choice == "q":
43         print("---------shopping list------------")
44         for index, ite in enumerate(shopping_mart):
45             print(index + 1, ite)
46         print("Your balance is: ¥", salary)
47         print("Welcome your next coming!")
48         print("Exit...")
49         exit()
50     else:
51         print("Invalid Choice!")
52         print("You are supposed to enter from 1 to", len(items))
程序清单

 

 

  程序效果

Please input your salary: 2

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 1
Your balance is not enough! Please try sth else.
Do you want to recharge?y
Please input money: 20000
Please wait for a while...
You have recharged successfully!
And the balance is:  20002 now!

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 1
You have bought ¥ !
Your balance is: ¥ 17202

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 
Invalid Choice!
You are supposed to enter from 1 to 3

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 2
You have bought ¥ !
Your balance is: ¥ 16902

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 
Invalid Choice!
You are supposed to enter from 1 to 3

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: 3
You have bought ¥ !
Your balance is: ¥ 16822

----------items----------
1. Huawei     ¥  2800
2. Earphone   ¥  300
3. Book       ¥  80
-------------------------

Please choose goods: q
---------shopping list------------
1 ['Huawei', '¥', 2800]
2 ['Earphone', '¥', 300]
3 ['Book', '¥', 80]
Your balance is: ¥ 16822
Welcome your next coming!
Exit...

Process finished with exit code 0

  

  

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python3基础知识

下一篇:Python学习第三天——三级目录