Python——購物車問題
阿新 • • 發佈:2018-11-21
enumerate pin money eas mon product list NPU lease
product_list=[(‘Mac‘,9000),
(‘kindle‘,800),
(‘tesla‘,900000),
(‘book‘,50),
(‘bike‘,2000)]
money=input(‘please input your money:‘)#輸入本金
shopping_car=[]
if money.isdigit():#saving是個字符串,字符串裏面是數字
money=int(money)
#將商品展示給用戶
#for i in product_list:
# print(product_list.index(i)+1,i)
#for i in enumerate(product_list,1):
# print(i)
while True:
for i,v in enumerate(product_list,1):
print(i, v)
choice=input(‘選擇購買商品編號[退出:q]:‘)
if choice.isdigit():
choice=int(choice)
if choice>0 and choice<=len(product_list):
p_item=product_list[choice-1]
if p_item[1]<money:
money-=p_item[1]
shopping_car.append(p_item)
else:
print(‘余額不足,還剩%s‘%money)
print(p_item)
else:
print(‘編號不存在‘)
elif choice==‘q‘:
print(‘--------------您已經購買如下商品--------------‘)
for i in shopping_car:
print(i)
print(‘您的余額為%s‘%money)
break
else:
print(‘invalid input‘)
Python——購物車問題