- 購物車程序:
需求:
- 啓動程序,讓用戶輸入工資,而後打印商品列表;
- 容許用戶根據商品編號購買商品;
- 用戶輸入商品列表後檢測餘額是否足夠,夠就直接扣款,不夠就提醒;
- 用戶能夠一直購買商品,也能夠直接退出,退出後打印已購買商品和餘額;
#第一題
salary = int(input('Please input your salary:'))
products = [['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike shoes',799]]
shopping_cart = list()
while True:
print('-------- 商品列表 --------')
for index,i in enumerate(products):
print('%s. %s %s'%(index,products[index][0],products[index][1]))
choice = input('Please input product num:')
if choice.isdigit():
choice = int(choice)
if choice >=0 and choice < len(products):
if salary >= products[choice][1]:
shopping_cart.append(products[choice])
salary -= products[choice][1]
else:
print('餘額不足')
print('----- 已購買商品 -----')
for index,p in enumerate(shopping_cart):
print('%s. %s %s'%(index,shopping_cart[index][0],shopping_cart[index][1]))
print('餘額',salary)
break
else:
print('商品不存在')
if choice == 'q':
print('----- 已購買商品 -----')
for index,p in enumerate(shopping_cart):
print('%s. %s %s'%(index,shopping_cart[index][0],shopping_cart[index][1]))
print('餘額',salary)
break