python複習購物車程序

我的學習總結:無他,惟手熟爾!多敲多練纔是王道

Python 第三課 元組的靈活運用&字符串的諸多操做

'''
複習購物車程序
內容 
1. 啓動程序後讓用戶輸入工資而後打印商品列表

2. 容許用戶根據商品編號購買商品

3. 用戶選擇商品後,檢測餘額是否充足,夠則直接扣款,不夠提醒

4. 可隨時退出,退出時,打印已經購買的商品和餘額

# 假定用戶不會輸錯
'''
shop_list = [['哇哈哈',10],['洗衣液',20],['老白乾',30]]
shop_car = []
money_left = 0
exit_shop = False

# 提示用戶輸入工資
salary = int(input("please input your salary:"))

while True:
     # 提示用戶商品列表
     for i in range(0,len(shop_list)):
          print(i,shop_list[i][0],shop_list[i][1])
          
     # 提示用戶選擇商品
     num = int(input("choose the goods>>>"))
     shop_car.append(shop_list[num])

     print("the goods in car>>",shop_car)

     while not exit_shop:
          # 計算商品總額
          sum_goods = 0
          for j in shop_car[:]:
               sum_goods += j[1]
          print("the sum if the goods:",sum_goods)

          # 檢查餘額
          money_left = salary - sum_goods
          # 餘額不足刪除某件商品
          if money_left < 0:
               print("you have no enough money")
               # 打印出已經在購物車的商品
               for i in range(0,len(shop_car)):
                    print(i,shop_car[i][0],shop_car[i][1])
               clear_num = int(input("drop the goods>>"))
               shop_car.pop(clear_num)
               print(shop_car)
          # 餘額充足直接扣款
          else:
               print("the money left>>>",money_left)
               exit_shop = True

     exit_shop = False

     choice = input("end press q>>>")
     if choice == 'q':
          print("------shop list------")
          for index,item in enumerate(shop_car):
               print(index,item)
          print("money left:",money_left)
          break          
               

 

MYK
2018年2月12日app

相關文章
相關標籤/搜索