1.列表与元组组合的形式存放商品
goods=[
('ipone',6999),
('macpro',12000),
('bike',4000),
('book',40),
('clothe',200),
('house',1000000)]
2.用户输入自己薪资后显示商品列表
salary=input('Please input your salary:')
salary=int(salary)
for index,item in enumerate(goods):
print(index+1,item)
3.初始化购物车为空,建立while循环:每次买完后还可以继续挑选
shopping_list=[]
while(True):
user_choice=input('Please select your choice:')
#用户选择判断,是否输入的是数字,如果是再判断是否超出价格
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice<=len(goods) and user_choice>0:
#再判断价格
if(salary>=goods[user_choice-1][1]):
shopping_list.append(goods[user_choice-1])
salary-=goods[user_choice-1][1]
print("you have bought the %s into your shopping cart,your current rest is %s!"%(goods[user_choice-1][0],salary))
continue
else:
print("your current balance is %s,cannot pay for it"%(salary))
else:
print("invalid option,please re-selected your goods!")
#如果输入不是数字再判断输入是否为q
elif user_choice=='q':
if(len(shopping_list)==0):
print("your shopping cart is empty!")
exit()
else:
print("----Following is your shopping cart---")
for item,good in enumerate(shopping_list):
print(item+1,good)
print("your current balence is %s"%(salary))
exit()
else:
print("invalid option!")
原文链接:https://blog.csdn.net/living_ren/article/details/78936628
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~