product_list = [
('Iphone', 5800),
('Mac Pro', 9800),
('Watch', 10600),
('Coffee', 31)
]
shopping_list = []
salary = input("Input your salary: ")
if salary.isdigit(): # 判断输入是否位数字
salary = int(salary)
while True:
for index, item in enumerate(product_list): # 取下标
print(index, item)
user_choice = input("选择要买嘛?>>")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary: # 买得起
shopping_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % ( p_item, salary))
else:
print("\033[41;1m你的余额只剩【%s】啦\033[0m" % (salary))
else:
print("商品不存在")
elif user_choice == "q":
print("-----shopping list -------")
for p in shopping_list:
print(p)
print("你的余额", salary)
exit()
else:
print("输入有误")
else:
print("输入有误")
python 购物练习
最新推荐文章于 2023-08-31 17:40:54 发布