python-ショッピングカート


作業要件:
  • ユーザーはまず自分の口座にお金をチャージします.例えば、3000元を先にチャージします.
  • ページ表示シーケンス番号+商品名+商品価格、例えば:1パソコン1999 2マウス10...nカート決済
  • ユーザーは選択した商品番号を入力し、商品名と商品価格を印刷し、この商品をカートに追加し、ユーザーは商品を追加し続けることができる.
  • ユーザーが入力した商品番号が間違っている場合は、入力が間違っていることを示し、再入力します.
  • ユーザーはnをカート決済として入力し、ユーザーのカートの中の商品、数量と単価を順次表示し、チャージした金額が不足すれば、ユーザーにある商品を削除させ、購入できるまで、チャージした金額が十分であれば、直接購入することができる.
  • ユーザはQまたはqを入力してプログラムを終了する.
  • プログラムを終了した後、ユーザーが購入した商品、数量、単価、および今回の合計消費金額、口座残高を順に表示します.
  • # auth_Mouni
    #date 2019-01-09
    
    product_lst = [
        {"name": "    ", "price": 1999},
        {"name": "    ", "price": 10},
        {"name": "    ", "price": 20},
        {"name": "    ", "price": 998}]
    shop_car = []
    cost_money = 0
    ID_lst = []  #            
    print("        ".center(15, "*"))
    while 1:
        salary = input("        :>>")
        if salary.isdigit():
            salary = int(salary)
            break
        else:
            print("      ,     ")
    
    chajia = 0
    while 1:
        print("       ".center(20, "*"), "
    ID", " ".center(9, " "), " ") for i in range(len(product_lst)): print(i, product_lst[i]["name"].center(11, " "), product_lst[i]["price"]) # # print(i + 1, product_lst[i]["name"].center(11, " "), product_lst[i]["price"]) # choise = input(" ID ,[N] [Q] :>>") if choise.isdigit(): # index = int(choise) # ID if int(index) < 0 or int(index) >= len(product_lst): print(" , :>>") continue else: for item in shop_car: if item["ID"] == index: item["totle"] += 1 break else: # ID shop_car.append( {"ID": index, 'name': product_lst[index]['name'], "price": product_lst[index]['price'], "totle": 1}) ID_lst.append(int(index)) elif choise.upper() == "Q": # print(" , ".center(15, "*")) man() elif choise == "n": # while 1: for i in range(len(shop_car)): # cost_money += shop_car[i]["price"] * shop_car[i]["totle"] while 1: # if len(shop_car) == 0: print(" ", "
    , %s " % (int(salary) - int(cost_money))) exit() if int(salary) > int(cost_money): # print(" ".center(20, "*"), "
    ", " ".center(10, " "), " ") for i in range(len(shop_car)): print(shop_car[i]["name"], str(shop_car[i]["totle"]).center(12, " "), shop_car[i]["price"]) print(" %s , %s " % (int(cost_money), int(salary) - int(cost_money))) exit() else: # chajia = int(salary) - int(cost_money) # print(" , , %s " % (chajia)) print("ID", " ".center(5, " "), " ".center(7, " "), " ") for i in range(len(shop_car)): # print(shop_car[i]["ID"], shop_car[i]["name"].center(7, " "), str(shop_car[i]["totle"]).center(9, " "), shop_car[i]["price"]) # while 1: choise = input(" ID, [Q]:>>") # ID if choise.isdigit(): choise = int(choise) # if choise >= 0 and choise < len(product_lst): # BUG if choise >= 0 and choise in ID_lst: # BUG break elif choise.upper() == "Q": print(" ".center(15, "*")) else: print(" ID , ") for item in shop_car: if item["ID"] == choise and item["totle"] == 1: cost_money -= item["price"] # 1 shop_car.remove(item) ID_lst.remove(int(item["ID"])) elif item["ID"] == choise: # cost_money -= item["price"] item["totle"] -= 1 ID_lst.remove(int(item["ID"])) # ID ID exit() else: print(" , :>>") product_lst = [ {"name": " ", "price": 1999,"ID":0}, {"name": " ", "price": 10,"ID":1}, {"name": " ", "price": 20,"ID":2}, {"name": " ", "price": 998,"ID":3}] shop_car = [] cost_money = 0 ID_lst = [] print(" ".center(15,"*")) while 1: salary = input(" :>>") if salary.isdigit(): salary = int(salary) break else: print(" , ") chajia = 0 while 1: print(" ".center(20, "*"), "
    ID", " ".center(9, " "), " ") for i in range(len(product_lst)): print(i, product_lst[i]["name"].center(11, " "), product_lst[i]["price"]) # # print(i + 1, product_lst[i]["name"].center(11, " "), product_lst[i]["price"]) # choise = input(" ID ,[N] [Q] :>>") if choise.isdigit(): # index = int(choise) # ID if int(index) < 0 or int(index) >= len(product_lst): print(" , :>>") continue else: for item in shop_car: if item["ID"] == index: item["totle"] += 1 ID_lst.append(product_lst[index]["ID"]) break else: # ID shop_car.append({"ID": index, 'name': product_lst[index]['name'], "price": product_lst[index]['price'],"totle": 1}) ID_lst.append(product_lst[index]["ID"]) elif choise.upper() == "Q": # print(" , ".center(15, "*")) elif choise == "n": # while 1: for i in range(len(shop_car)): # cost_money += shop_car[i]["price"] * shop_car[i]["totle"] while 1: # if len(shop_car) == 0: print(" ") print(" ","
    , %s " % (int(salary) - int(cost_money))) # man() if int(salary) > int(cost_money): # print(" ".center(20, "*"), "
    ", " ".center(10, " "), " ") for i in range(len(shop_car)): print(shop_car[i]["name"], str(shop_car[i]["totle"]).center(12, " "), shop_car[i]["price"]) print(" %s , %s " % (int(cost_money), int(salary) - int(cost_money))) exit() else: # chajia = int(salary) - int(cost_money) # print(" , , %s " % (chajia)) print("ID", " ".center(5, " "), " ".center(7, " "), " ") for i in range(len(shop_car)): # print(shop_car[i]["ID"], shop_car[i]["name"].center(7, " "), str(shop_car[i]["totle"]).center(9, " "), shop_car[i]["price"]) # while 1: choise = input(" ID, [Q]:>>") # ID if choise.isdigit(): choise = int(choise) if choise >= 0 and choise in ID_lst: # BUG break else: print(" ID , ") continue elif choise.upper() == "Q": # man() else: print(" ID , ") for item in shop_car: if item["ID"] == choise and item["totle"] == 1: cost_money -= item["price"] # 1 shop_car.remove(item) ID_lst.remove(int(item["ID"])) elif item["ID"] == choise: # cost_money -= item["price"] item["totle"] -= 1 ID_lst.remove(int(item["ID"])) exit() else: print(" , :>>")