product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',31),
('Alex Python',120),
]
shopping_list = []
salary = input("请输入你的工资:")
if salary.isdigit() :
salary = int(salary)
while True:
if len(shopping_list) == 0:
print("你还没有购买商品")
else:
print("你已经购买的商品有",shopping_list)
for k,y in enumerate(product_list):
print("编号:{},商品详情:{}".format(k, y))
user_choice = input("请输入商品编号:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice > (len(product_list)) or user_choice < 0:
print("商品编号不存在!")
continue
else:
if salary < int(product_list[user_choice][1]):
print("你的余额还有{}不够了,赶紧走吧!".format(salary))
break
else:
salary -= int(product_list[user_choice][1])
shopping_list.append(product_list[user_choice])
print("你还剩下{}钱".format(salary))
else:
print("请输入正确的商品编号")
continue
else:
print("请输入正确的工资数额")
知识点:
salary.isdigit(),判断是否为数字组成
enumerate(product_list),函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标