Python 購物車

來源:互聯網
上載者:User

標籤: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 購物車

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.