Python小代码_3_购物车
product_list = [ ('MacBook', 9000), ('kindle', 500), ('tesla', 900000), ('book', 100), ('bike', 2000), ] saving = input("please input your money:") shopping_car = [] if saving.isdigit(): saving = int(saving) while True: #打印商品内容 for i, v in enumerate(product_list, 1): print(i, ">>>", v) #引导用户选择商品 choice = input("choose goods that you want to buy[exit:q]:") #验证输入是否合法 if choice.isdigit(): choice = int(choice) if choice > 0 and choice <= len(product_list): #将用户选择商品通过choice选出 p_item = product_list[choice - 1] #如果钱足够,用saving减去商品价格,并将该商品加入购物车 if p_item[1] <= saving: saving -= p_item[1] shopping_car.append(p_item) else: print("-----------------") print("Sorry, your balance is not enough.") print("Your balance: " + str(saving)) print("-----------------") continue print("-----------------") print("You have chose " + p_item[0]) print("Your balance: " + str(saving)) print("-----------------") else: print("Non existent") elif choice == 'q': print("---------------") print("You have chose the following goods:") print("Goods\t\tnumber\tPrice") num = 1 #循环遍历购物车里面的商品,购物车存放的是已买商品 for i in shopping_car: print(i[0] + "\t\t" + str(num) + "\t\t" + str(i[1])) print() print("balance :", saving) print("---------------") break else: print("invalid input")
本博客内容已全部迁移到我的个人网站 木子窗明(www.muzicm.cn),以后都只会在我的个人网站上首发博客,详情请查看我的个人网站。