首页 > 代码库 > 购物车练习
购物车练习
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")
购物车练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。