第二周练习 part1 --- 简易版购物车程序

2018-06-18 01:49:08来源:未知 阅读 ()

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

#购物车程序

 1 '''启动程序后让用户输入工资,然后打印商品列表
 2 用户根据编号选择购买的商品
 3 用户的钱足够就直接扣钱,不够就提醒
 4 可随时退出,退出时打印已购买商品和余额'''
 5 
 6 salary=input("请输入你本月的工资: ")
 7 goods=['1.可乐(12)','2.雪碧(13)','3.Notebook(5)','4.iPhone(8888)','5.Earphone(3643)']
 8 price=[12, 13, 5, 8888, 3643]
 9 cost=0
10 bought = []
11 startshop = 1
12 k = 0
13 print(goods)
14 while startshop == 1:
15     i = input('Please type the goods number of what you want to buy, when you are done, type "0" to quit: ')
16     if int(i) == 0:
17         startshop = 0
18     else:
19         if price[int(i) - 1] + cost > int(salary):
20             print("You don't have enough money.")
21             j = input('Do you want to buy other goods? Type 1 for yes, type 0 for quit: ')
22             if int(j) == 1:
23                 continue
24             if int(j) == 0:
25                 startshop = 0
26         else:
27             cost = cost + price[int(i) - 1]
28             bought.insert(k,goods[int(i)-1])
29             k+= 1
30 print("You have bought ", bought[:], " and cost ", cost, " yuan.", "Now you have", int(salary) - cost, "yuan left")

-------------------------------------------------New Version---------------------------------------------------
 
 1 salary=input("Please input your salary: ")
 2 if salary.isdigit():
 3     salary = int(salary)
 4     goods=['Coco(12 yuan)','Sprite(13 yuan)','Notebook(5 yuan)','iPhone(8888 yuan)','Earphone(3643 yuan)']
 5     price=[12, 13, 5, 8888, 3643]
 6     cost=0
 7     bought = []
 8     startshop = 1
 9     k = 0
10     for a in enumerate(goods): print(a)
11     while True:
12         i = input('Please type the goods number of what you want to buy, when you are done, type "q" to quit>>> ')
13         if i.isdigit():
14             i = int(i)
15             if i > 4 or i < 0:
16                 print('Pleasure type the number shown before the merchandise.')
17                 continue
18             if price[i] + cost > int(salary):
19                 print("You don't have enough money.")
20                 while True:
21                     j = input('Do you want to buy other goods? Type y for yes, type q for quit: ')
22                     if j == 'y':
23                         break
24                     elif j == 'q':
25                         print("You have bought")
26                         for w in bought:
27                             print(w)
28                         print("and cost", cost, "yuan.", "Now you have", salary - cost, "yuan left.")
29                         exit()
30                     else:
31                         print('\033[41;1mPleasure type "y" or "q"!\033[0m')
32                         continue
33             else:
34                 cost = cost + price[i]
35                 bought.insert(k, goods[i])
36                 k += 1
37                 print('Added', goods[i], 'to your list successfully. Now you have', salary - cost, 'yuan left.')
38         elif i == 'q':
39             print("You have bought")
40             for w in bought:
41                 print(w)
42             print("and cost", cost, "yuan.")
43             print("Now you have", salary - cost, "yuan left.")
44             exit()

 

 

标签:

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

上一篇:算法——python实现快速排序(二分法思想)

下一篇:初遇python,python基础