简易购物程序的实现。(最初版)
1 #简易购物程序 2 3 money = 20000 4 goods_list = [‘IPhone 8‘, ‘Bicycle‘, ‘Banana‘, ‘Xiaomi Note‘, ‘MacBook‘, ‘矿泉水‘] 5 goods_price = [9000, 800, 20, 3200, 12000, 5] 6 print("欢迎来到智能购物中心!您卡上余额共有%d元。"% money) 7 print() 8 print(‘商品信息:‘) 9 count = 0 10 while count < len(goods_list): 11 print(‘商品编号:%d‘ % count + " " + goods_list[count] + ‘----‘ + str(goods_price[count]) + ‘元‘,end="\\n") 12 count += 1 13 print() 14 flag = True 15 buy_lists = [] 16 buy_price = [] 17 while flag: 18 print() 19 num = int(input("请输入要购买商品的编号:")) 20 print() 21 if num < len(goods_list): 22 if num == 0 and money >= goods_price[num]: 23 money -= int(goods_price[num]) 24 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 25 buy_lists.append(goods_list[num]) 26 buy_price.append(goods_price[num]) 27 elif num == 1 and money >= goods_price[num]: 28 money -= int(goods_price[num]) 29 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 30 buy_lists.append(goods_list[num]) 31 buy_price.append(goods_price[num]) 32 elif num == 2 and money >= goods_price[num]: 33 money -= int(goods_price[num]) 34 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 35 buy_lists.append(goods_list[num]) 36 buy_price.append(goods_price[num]) 37 elif num == 3 and money >= goods_price[num]: 38 money -= int(goods_price[num]) 39 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 40 buy_lists.append(goods_list[num]) 41 buy_price.append(goods_price[num]) 42 elif num == 4 and money >= goods_price[num]: 43 money -= int(goods_price[num]) 44 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 45 buy_lists.append(goods_list[num]) 46 buy_price.append(goods_price[num]) 47 elif num == 5 and money >= goods_price[num]: 48 money -= int(goods_price[num]) 49 print("您购买了%s" % goods_list[num], ‘剩余%d元‘ % money) 50 buy_lists.append(goods_list[num]) 51 buy_price.append(goods_price[num]) 52 else: 53 print("余额不足!") 54 flag = False 55 else: 56 print("请输入正确的商品编号!") 57 print() 58 for m in range(len(buy_lists)): 59 buy_lists[m] += ‘--‘ + str(buy_price[m]) + ‘元‘ 60 print("您共购买了%d件商品:%s" % (len(buy_lists),‘ , ‘.join(buy_lists)) + ‘\\n‘ + ‘您卡上剩余%d元‘ %(money) + ‘\\n‘ + ‘欢迎下次光临!‘)
运行结果: