第二周练习 part3 --- 简易版Shopping Mall

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

浙公网安备 33010602011771号