这个程序主要是使用了条件语句,字符串操作,字典,列表
import string print("欢迎进入购物车") goods=["汽车:1元","自行车:2元","飞机:3元"] goods_name=['汽车','自行车','飞机'] goods_prices={"汽车":1,"自行车":2,"飞机":3} solar=int(input("请输入你的工资额度")) unchange_solar=solar buy_goods_list = [] if solar > 0: for i in range(10): print("可用金额:%d"%(solar)) print(goods) buy_goods=input("请选择需要购买的商品:") buy_goods=buy_goods.replace(' ','') if not buy_goods in goods_name: print("该商品不存在,请重新选择") continue solar=solar-goods_prices[buy_goods] if solar < 0: print("对不起,你的余额不做,请重新选购") solar=unchange_solar continue buy_goods_list.append(buy_goods) print("可用金额:%d" % (solar)) print("已购买的商品:%s"%(buy_goods_list)) chance=input("是否继续购买:[y/n]") if chance=="y": continue elif chance=="n": break print(buy_goods_list) else: print("输入错误,请重新输入")