首页 > 代码库 > 购物车练习

购物车练习

goods=[
("iphone",5800),
("iwatch",2300),
("Huawei P9",4300),
("Mac Pro",12000),
("Book",500)
]########################商品列表
shoping_list=[]##########空表
salary=input("input You are Salary:")
if salary.isdigit():###########.isdigit整数
salary=int(salary)#####转数据为int
while True:####循环
for index,item in enumerate(goods):########虚幻index,给goods赋值
print(index,item)
user_choice=input("please choice goods...")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice<len(goods) and user_choice>=0:
p_item=goods[user_choice]
if p_item[1]<=salary:
shoping_list.append(p_item)
salary-=p_item[1]
print("Added %s into shopping cart ,your current balanceis \033[31;1m%s\033[0m"%(p_item,salary))
else:
print("\033[41;1m你的余额不住,只剩[%s]\033[0m"%salary)
else:
print("product code [%s] is not exist!" % user_choice)
elif user_choice=="q":
print("---------------shopping list------------------")
for p in shoping_list:
print(p)
print("your current balance",salary)
exit()
else:
print("invalid option")


购物车练习