# Author:南邮吴亦凡
# 商品列表
product_list = [
('Iphone',5800), # 逗号一定不可以省略!
('Mac',4800),
('smartphone',400),
('watch',9100),
('coffee',500),
('python',9200),
]
shopping_list = []
# 工资
salary = input("Input your salary:") # 输入的是字符串
if salary.isdigit(): # 如果这是一个数字,就转成数字
salary = int(salary)
while True:
# for item in product_list:
print(item) # 无序号
# for item in product_list:
print(product_list.index(item),item) # 有序号(1)
for index,item in enumerate(product_list):
print(index,item) # 有序号(2)
# 选择买的商品
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))
# print("Added %s into shopping cart,your current balance is %s" %(p_item,salary)) # 余额没有颜色
else: # 买不起
print("\033[41;1m你的余额只剩[%s]啦\033[0m" % salary) # 31无背景的红色,41有背景的红色
print("商品[%s]不存在")
elif user_choice =='q':
print("-----shopping list------")
for p in shopping_list:
print(p)
print("your current balance:",salary)
exit()
else:
print("invalid option")
|
请发表评论