pyhton简单的购物车编码

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 33[31;1m%s33[0m" %(p_itam,salary) )
else:
print("33[41;1m你的余额只剩[%s]啦,余额不足33[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")
原文地址:https://www.cnblogs.com/boosli/p/15247039.html