标签:col ike ase nbsp code money bre bsp dig
# -*- coding: utf-8 -*- # @Time : 2020/7/26 14:13 # @Author : Breeze # @FileName: 购物车程序.py product_list = [ ("Mac",9000), ("kindle",800), ("tesla",900000), ("python book",105), ("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) # print(product_list.index(i)+1,i) #引导用户选择商品 choice = input("选择购买商品编号[退出: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("余额不足,还剩%s"%saving) print(p_item) else: print("编码不存在") elif choice == ‘q‘: print("--------您已购买以下商品--------") #循环遍历购物车里的商品,购物车存放的是已买商品 for i in shopping_car: print(i) print("您还剩%s元"%saving) break else: print("Invalid input") else: print("must be digit!")
标签:col ike ase nbsp code money bre bsp dig
原文地址:https://www.cnblogs.com/wxx123/p/13384824.html