购物车的实例
Posted cy2268540857
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车的实例相关的知识,希望对你有一定的参考价值。
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 is 33[31:1m %s 33[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")
以上是关于购物车的实例的主要内容,如果未能解决你的问题,请参考以下文章