惜&沙卡

 

元组--购物车实战

不可变的列表,作用:存不可变的信息

实例:

启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额

product_list = [('Iphone', 6800), ('Watch', 10060), ('coffce', 31), ('bike', 800)]
shopping_list = []
salary = input("please input 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("Add %s into shopping_cart,your currently balance is \033[31;1m %s \033[0m" % (p_item, salary))
else:
print("\033[41;1m你的余额只剩[%s]啦\033[0m,余额不足" % salary)
else:
print("product_code %s is not exist" % salary)
elif user_choice == 'q':
print("-------shopping_list---------------")
for p in shopping_list:
print(p)
print("your currently balance is %s" % salary)
exit()
else:
print("Invalid choice")
else:
print("salary %s is not a number" % salary)

posted on 2019-12-28 17:16  惜&沙卡  阅读(68)  评论(0编辑  收藏  举报

导航