Python入门:购物车实例

product_list=[('iphone',5800),
('pro',120000),
('python book',120),
('Bike',800),
('coffe',39)
] #定义商品列表

shopping_list=[] #定义购物车
salary=input("your sally:") #输入薪水
if salary.isdigit(): #判断是否为数字
salary=int(salary)
while True:
for index,item in enumerate(product_list): #enumerate 取下标和内容
print(index,item)
user_choice=input("please you choose product:") #输入商品编号
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 salary>=p_item[1]:
shopping_list.append(p_item)
salary=salary-p_item[1]
print("added %s into shopping cart,your current balance is %s"%(p_item,salary))
else:
print("你的余额只剩 %s ,不能买了" %salary)
else:
print("您选择的不存在")
elif user_choice=="q": #输入q 退出循环
print("-------------shopping list----------")
for i in shopping_list: #遍历打印购物车
print(i)
exit()
else:
print("Invalid option")
else:
print("your salary is wrong")

猜你喜欢

转载自www.cnblogs.com/luckerzhang/p/9089388.html