Python簡單的購物車程序

#Author:xuhan
# -*- coding:utf-8 -*-
shopping_car = []
while True:
   salary = input('[q=quit]請輸入您的購物預算:')
   if salary.isdigit():
       break
   else
:
       continue
print(' 歡 迎 光 臨 家 樂 福 購 物 中 心 '.center(60,'-'))
product_list = [('Iphone7',5999),('Mac Are',6999),('UNIQL',200)]
choice_flag = ''
while choice_flag != 'q':   #判斷用戶選擇購物還是離開
   
for product_item in range(len(product_list)):
       print(product_item,product_list[product_item][0],':',product_list[product_item][1]) #打印商品信息
   
choice=input('請輸入您想購買的產品序號[或輸q鍵結束購物]:')
   if choice.isdigit() :
       if int(choice) < len(product_list):     #判斷所選擇的產品是否存在
           
if int(salary)>=int(product_list[int(choice)][1]):  #判斷餘額是否足夠支付選擇的產品
               
shopping_car.append(product_list[int(choice)][0])   #如果足夠就添加到購物車
               
salary=int(salary)-int(product_list[int(choice)][1])    #從餘額扣除所選商品的價格
               
print('已購買商品:',shopping_car)    #打印購物車內產品
           
else:
               print('對不起,您的餘額不足,剩餘:',salary,'元!') #餘額不足進行提醒
               
continue
       else
:
           print('對不起,您選擇的商品暫時不存在!')   #如果不存在進行提醒
   
elif choice !='q':  #選擇結束購物
       
continue
   else
:
       print('*'*21)
       print(' 再見,歡迎下次光臨! ')
       print('*' * 21)
       break

執行效果:

wKioL1hPZG7A4LbRAACbFUaJ6cs303.png-wh_50

wKiom1hPZLPTEtvvAACK8rP10ms650.png-wh_50

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章