购物车的实例

2020-02-17
_author_ = "chen"
product_list = [
('iphone', 5800),
('mac book', 9800),
('bike', 800),
('watch', 10600),
('coffee', 31),
('python', 120),

]
shopping_list = []
salary = input("input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index, item in enumerate(product_list): # enumerate取下标
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 is33[31:1m %s33[0m" % (
p_item, salary)) # 打印颜色
else:
print(":33[41;1m你的余额只剩[%s]啦,买毛线啊33[0m" % salary)
else:
print("product code [%s] is not exist!" % user_choice)
elif user_choice == 'q':
print("------shopping list------")
for p in shopping_list:
print(p)
print("your current balance", salary)
break
else:
print("invalid option")
原文地址:https://www.cnblogs.com/cy2268540857/p/12322690.html