product_list = [
('IphoneX',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',31),
('book',10),
('test',120),
]
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_chiose = input("请选择商品")
if user_chiose.isdigit():
user_chiose = input(user_chiose)
if user_chiose < len(product_list) and user_chiose >=0:
p_itam = product_list[user_chiose]
if p_itam[1]<= salary:
shopping_list.append(p_itam)
salary = salary-p_itam[1]
print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_itam,salary) )
else:
print("\033[41;1m你的余额只剩[%s]啦,余额不足\033[0m" % salary)
else:
print("product code [%s] is not exist!"% user_chiose)
elif user_chiose == 'q':
print("--------shopping list------")
for p in shopping_list:
print(p)
print("Your current balance:", salary)
exit()
else:
print("invalid option")