購物車練習
阿新 • • 發佈:2017-08-07
lan elif 賦值 invalid 循環 watch append pro iwatch
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")
購物車練習