2019.5.16 课后练习。简易购物车程序
Posted kentee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019.5.16 课后练习。简易购物车程序相关的知识,希望对你有一定的参考价值。
根据之前看过的知识点,以自己的思路先写出了一个程序,后续继续老师的操作再进行优化。
1 salary = int(input("请输入您的工资(单位 元):")) 2 3 print("以下是可购清单:") 4 5 shopping_list = [‘‘,‘iphone‘,‘computer‘,‘book‘,‘apple_juice‘,‘bread‘,‘cake‘]#商品列表 6 commodity_prices = [0,6000,7000,2,3,3,5] #商品价格列表 7 already_buy = []#已经购买的商品列表 8 9 print("1.",shopping_list[1],commodity_prices[1],"元") 10 print("2.",shopping_list[2],commodity_prices[2],"元") 11 print("3.",shopping_list[3],commodity_prices[3],"元") 12 print("4.",shopping_list[4],commodity_prices[4],"元") 13 print("5.",shopping_list[5],commodity_prices[5],"元") 14 print("6.",shopping_list[6],commodity_prices[6],"元") 15 16 select = "" 17 while select != "quit": 18 select = input("购买您需要的宝贝至购物车,请输入数字(如需结算,输入 quit):") 19 print() 20 if select == "1": 21 salary = salary - commodity_prices[1] 22 if salary < 0: 23 print("余额不足,",salary) 24 salary = salary + commodity_prices[1] 25 else: 26 print("已加入",shopping_list[1],"到你的购物车,当前余额:",salary) 27 already_buy.append(shopping_list[1]) 28 elif select == "2": 29 salary = salary - commodity_prices[2] 30 if salary < 0: 31 print("余额不足,", salary) 32 salary = salary + commodity_prices[2] 33 else: 34 print("已加入", shopping_list[2], "到你的购物车,当前余额:", salary) 35 already_buy.append(shopping_list[2]) 36 elif select == "3": 37 salary = salary - commodity_prices[3] 38 if salary < 0: 39 print("余额不足,", salary) 40 salary = salary + commodity_prices[3] 41 else: 42 print("已加入", shopping_list[3], "到你的购物车,当前余额:", salary) 43 already_buy.append(shopping_list[3]) 44 elif select == "4": 45 salary = salary - commodity_prices[4] 46 if salary < 0: 47 print("余额不足,", salary) 48 salary = salary + commodity_prices[4] 49 else: 50 print("已加入", shopping_list[4], "到你的购物车,当前余额:", salary) 51 already_buy.append(shopping_list[4]) 52 elif select == "5": 53 salary = salary - commodity_prices[5] 54 if salary < 0: 55 print("余额不足,", salary) 56 salary = salary + commodity_prices[5] 57 else: 58 print("已加入", shopping_list[5], "到你的购物车,当前余额:", salary) 59 already_buy.append(shopping_list[5]) 60 elif select == "6": 61 salary = salary - commodity_prices[6] 62 if salary < 0: 63 print("余额不足,", salary) 64 salary = salary + commodity_prices[6] 65 else: 66 print("已加入", shopping_list[6], "到你的购物车,当前余额:", salary) 67 already_buy.append(shopping_list[6]) 68 else: 69 print("您已购买以下商品:") 70 print(already_buy) 71 print("余额为:",salary) 72 print("谢谢惠顾,欢迎下次光临!")
以上是关于2019.5.16 课后练习。简易购物车程序的主要内容,如果未能解决你的问题,请参考以下文章