Python 購物車練習 2.0
阿新 • • 發佈:2018-10-04
iphone6 iphone 當前 product art cycle while 商品 enume
product_list = [
[‘iphone6s‘, 5800],
[‘mac book‘, 9000],
[‘coffee‘, 32],
[‘python book‘, 80],
[‘bicycle‘, 1500]
]
shopping_cart = []
while True:
salary = input("Salary(整數) = ")
if salary.isdigit():
salary = int(salary)
break
else:
print(‘請輸入正確的數字‘)
while True:
for i, j in enumerate(product_list, 1):
print(i, j)
print(‘當前余額:‘, salary)
choice = input(‘請輸入你想購買的商品編號【q退出】:‘)
if choice.isdigit():
choice = int(choice)
if 0 < choice < len(product_list) + 1:
if salary >= int(product_list[choice-1][1]):
print(product_list[choice-1][0], ‘已經加入您的購物車,當前余額:‘, salary - int(product_list[choice-1][1]))
shopping_cart.append(product_list[choice-1])
salary = salary - int(product_list[choice - 1][1])
else:
print(‘余額不足:‘, salary - int(product_list[choice-1][1]))
else:
print(‘編號錯誤‘)
elif choice == ‘q‘:
print(‘ 已購買商品 金額‘)
for i, j in enumerate(shopping_cart, 1):
print(i, j)
print(‘你的余額為:‘, salary, ‘元。歡迎再次光臨!‘)
break
else:
print(‘請輸入正確的信息‘)
Python 購物車練習 2.0