product_list = [
('iphone',5000),
('coffee',31),
('bicyle',888),
('iwatch',2666),
('Mac Pro',12000),
('book',88)
] #列表显示商场商品
shopping_car = [] #用个空列表来充当购物车,
salary = input("请输入你的金币:")
if salary.isdigit(): #判断输入是否为数字,真则执行
salary = int(salary)
else:
print("警告!!!! 请输入有效金额!。。。")
while True:
print("----------本商城商品--------")
for i,v in enumerate(product_list): #用两个参数接受商场信息
print(i,"--",v)
user_choice = input("请输入需要购买的商品【退出:q】:")
if user_choice.isdigit():#判断用户选择是否为数字
user_choice = int(user_choice)
if user_choice>= 0 and user_choice<len(product_list):#需要大于0或小于商城长度
buy_p = product_list[user_choice]#选择商品赋值
if buy_p[1] < salary:#判断选择商品的价格与用户金币关系
shopping_car.append(buy_p)#把购买物品加入购物车
salary = salary - buy_p[1]#剩余金币
# print("您已购买%s,"%user_choice[0])
else:
print("金额不足 买不起!!余额%s"%salary)
else:
print("没有此商品!")
elif user_choice == 'q':
print("您购买商品如下:")
for i in shopping_car:
print(i)
print('余额%s'%salary)
break
else:
print("wrong!")
python简记4--购物车程序
猜你喜欢
转载自blog.csdn.net/baidu_18374165/article/details/81505356
今日推荐
周排行