1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Author:Mgin 4 shopping_car = [] 5 salary = int(input("your salary:")) 6 goods_list = [["Watch",20000],["ThinkPad",8500],["IPhone",5800 ],["Suit",3700],["Boost shoes",1700],["Contact Lens",150],["Alex python",80],["Starbucks",30]] 7 print("goods list:") 8 for i in goods_list: 9 print(goods_list.index(i),i) 10 while salary > 0: 11 add = int(input("add goods to your shopping car:")) 12 if add > int(len(goods_list)-1) or add < 0: 13 print("goods have not been found ") 14 stop = input("Do you want to continue:") 15 if stop != ‘n‘: 16 continue 17 else: 18 for i in shopping_car: 19 print(i) 20 print("balance:", salary) 21 break 22 elif salary - goods_list[add][1] > 0: 23 salary = salary - goods_list[add][1] 24 shopping_car.append(goods_list[add]) 25 print("added",goods_list[add][0],"to your shopping car.","\n","balance:",salary) 26 stop = input("Do you want to continue:") 27 if stop != ‘n‘: 28 continue 29 else: 30 for i in shopping_car: 31 print(i) 32 print("balance:",salary) 33 break 34 else: 35 print("you have not enough money") 36 stop = input("Do you want to continue:") 37 if stop != ‘n‘: 38 continue 39 else: 40 for i in shopping_car: 41 print(i) 42 print("balance:", salary) 43 break
程序:购物车
需求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4.可随时退出,退出时,打印已购买商品和余额
时间: 2024-11-09 02:49:06