標籤:art [] pen int choice 迴圈 app car 選擇
product_list = [
(‘iphone‘,8000),
(‘Mac Pro‘,9800),
("Bike",3000),
("Watch",10000),
("Cofee",30),
("Book",100),
] #定義商品列表
shoppint_list = [] #購物車空列表
while True:
salary = input("Input you salary:") #輸入預算
if salary.isdigit(): #isdigit 整數
salary = int (salary)
while True: #迴圈
# for item in product_list:
# print(product_list.index(item),item)
for index,item in enumerate(product_list): #下標做產品編號、 enumerate取出列表下標
print(index,item) #index下標、item資料
user_choice = input("選擇買什麼東西>>>:") #使用者輸入
if user_choice.isdigit(): #整數編碼
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >-0: #商品取值範圍
p_item = product_list[user_choice]
if p_item[1] <= salary :
shoppint_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart,your current balance is %s" %(p_item,salary) )
else:
print("\033[41;1m你的餘額只剩[%s]了,還買個雞腿\033[0m" %(salary)) # "\033[[\033[Om" 加亮色
else:
print("商品不存在")
elif user_choice == ‘q‘:
print("----商品列表------")
for p in shoppint_list:
print(p)
print("you current balance:",salary)
exit()
else:
print("invalid option")
else:
print("請輸入數字")
Python 購物車