購物車

流程圖:



#! /usr/bin/env python
# -*- coding:utf-8 -*-
import json
import os
user_name = "lifei"
user_passwd = "123"
user_account = 0
shop_car = []
jiesuan_list = []
flag = False
N = 0
while flag is not True:
   user = input("Input your username:").strip()
   if N > 2:  # 輸錯三次強制退出
       exit("login disabled")
   passwd = input("Input your password:").strip()
   if passwd == user_passwd and user == user_name:
       print("welcome %s login." % user)
       flag = True
   else:
       print("Invalid username or password!")
       N += 1
data_list = [
   "1.購物歷史",
   "2.充值",
   "3.購物",
   "4.結算中心",
]
product_list = [
   ('iphone', 4800),
   ('mac_book', 8888),
   ('coffee', 30),
   ('tesla', 820000),
   ('bike', 800),
]
exit_flag = False
while not exit_flag:
   print(data_list)
   choice = input("[quit] or make your choice:").strip()
   if choice == "1":
       if os.path.exists('data.json'):
#            print("shop_infomation".center(50, '*'))
#            f = open("data.json", "r")
#            shop_history = f.read()
#            print(shop_history)
#            f.close()
           with open("data.json", "r", encoding="UTF-8") as f_load:
               r_load = json.load(f_load)
           print(r_load[user])
           print("end".center(50, '*'))
       else:
           print("no shop history.")
   elif choice == "2":
       money_chongzhi = int(input("input your money:").strip())
       user_account += money_chongzhi
       print("now your account:%s." % user_account)
   elif choice == "3":
       print("product list".center(50, '-'))
       for item in enumerate(product_list):
           index = item[0]
           p_name = item[1][0]
           p_price = item[1][1]
           print(index, '.', p_name, p_price)
       exit_flag1 = False
       while not exit_flag1:
           user_choice = input("[quit],what do you want to buy?:").strip()
           if user_choice.isdigit():
               nums = int(input("how many do you want to buy?:").strip())
               user_choice = int(user_choice)
               if user_choice <= len(product_list):
                   p_item = product_list[user_choice]
                   one_price = nums*p_item[1]
                   # print(one_price)
                   if one_price <= int(user_account):
                       for n2 in range(0,nums):
                           shop_car.append(p_item)
                          # print("目前購物車中已添加的商品:%s" % shop_car)
                   else:
                       print("your account can not afford.")
           else:
               if user_choice == 'q' or user_choice == 'quit':
                   print("shopcar products as below".center(40, '*'))
                   for item in shop_car:
                       print(item)
                   exit_flag1 = True
                   print("end".center(40, '*'))
   elif choice == "4":
       for item in shop_car:
           print(item)
       summary = 0
       n = len(shop_car)
       for n1 in range(0, n):
           summary += shop_car[n1][1]
       print("總消費金額:%s" % summary)
       if summary <= int(user_account):
          user_account = int(user_account) - summary
          print("還剩餘金額:%s." % user_account)
          jiesuan_list.extend(shop_car)
#           print(jiesuan_list)
          dic = {
              "lifei": jiesuan_list
          }
          print(dic)
          with open("data.json", "w", encoding="UTF-8") as f_dump:
              s_dump = json.dump(dic, f_dump, ensure_ascii=False)
       else:
           print("餘額不足")
   elif choice == "q" or choice == "quit":
       print("purchase products as below".center(40, '*'))
       for item in jiesuan_list:
           print(item)
       exit_flag = True
   else:
       print("\033[1;31;40minvalidtype\033[0m")


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