Python3實現購物車功能

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了Python3實現購物車功能,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文執行個體為大家分享了Python3實現購物車功能的具體代碼,供大家參考,具體內容如下

購物車要求:

1、啟動程式後,輸入使用者名稱密碼後,如果是第一次登入,讓使用者輸入工資,然後列印商品列表

2、允許使用者根據商品編號購買商品

3、使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒

4、可隨時退出,退出時,列印已購買商品和餘額

5、在使用者使用過程中, 關鍵輸出,如餘額,商品已加入購物車等訊息,需高亮顯示

6、使用者下一次登入後,輸入使用者名稱密碼,直接回到上次的狀態,即上次消費的餘額什麼的還是那些,再次登入可繼續購買

7、允許查詢之前的消費記錄

邏輯圖:

執行代碼:

#!/usr/bin/env python3# Author: Robert# --*-- coding: utf-8 --*--set = False  #設定set 當輸入為q就可以退出file = open("購物車使用者資訊檔案.txt","r+",encoding="utf-8")  #讀取購物車使用者資訊檔f = str(file.read())  #將檔案內容轉化成字串for line in f:  file_str = str(f)data = eval(file_str)  #將字串轉換為字典dataname = input("輸入姓名:")password = input("輸入密碼:")while True:  if name in data:      #使用者在檔案中    if password in data[name]:     #密碼和使用者名稱對應,校正正確,登入。      salary = float(data[name][password])      print('''\033[32;1m歡迎登入,當前餘額為%s\033[0m'''%salary)      break    else:      #否則密碼錯誤,請重新輸入      password = input("密碼錯誤,請重新輸入:")      continue  else:      #否則判斷為首次登入,將使用者名稱,密碼,工資存到使用者資訊檔中    password_salary = {}    salary_str = input("歡迎首次登入,請輸入你的工資:")    salary = float(salary_str)    password_salary[password] = salary     #工資對應到密碼    data[name] = password_salary      #將密碼-工資對應到使用者名稱    file.seek(0)    file.write(str(data))    file.tell()    breaklist = [#購物清單  ["iphone",5800],  ["sifei",800],  ["macbook",17500],  ["book",75],  ["apple",5]]file_list_r = open("曆史購物資訊.txt","r+",encoding="utf-8")file_list_r = str(file_list_r.read())shoppinglist_dict = eval(file_list_r)if name not in shoppinglist_dict:  shoppinglist_dict[name] = []shoppinglist = shoppinglist_dict[name]shoppinglist_dict_now = []choose = input("\n是否需要查詢曆史購物記錄(y/n):")if choose == 'y':  print("\n\n---------->曆史購物記錄<----------")  print(shoppinglist)  print("---------->結束<----------")while not set:   #購物車開始  print("---------->商品清單<----------")  for index,item in enumerate(list,1):    print(index,item)  print("---------->結束<----------")  number = input("請輸入想購買商品編號:")  if number == "q":    set = True    data[name][password] = str(salary)    file.seek(0)    file.write(str(data))    file.tell()    print("---------->購物清單<----------")    print(shoppinglist)    print("您的餘額:",salary)    print("---------->結束<----------")    shoppinglist.extend(shoppinglist)    shoppinglist_dict[name] = shoppinglist  elif number.isdigit() == False:    print("\033[31;1m輸入不是編號內容,請重新輸入\033[0m")  elif int(number)>int(len(list)) or int(number)<= 0:     #輸入值不在清單中,報錯    print("\033[31;1m您所購買的商品不在清單中\033[0m")  else:    number_buy = int(number)-1    if list[number_buy][1]<(salary):      #如果餘額足夠,提示購買成功並顯示餘額。      salary = salary - int(list[number_buy][1])      msg = '\033[32;1m您已經將%s加入購物車中,餘額為%d\033[0m'%(list[number_buy][0],salary)      print(msg)      shoppinglist.append(list[number_buy])      #將本次購物資訊加到購買記錄中    else:      print("\033[31;1m餘額不足,無法購買!\033[0m")      #提示餘額不足

購物車使用者資訊檔案.txt

{'name': {'password': '10000'}, 'cx': {'123': '725.0'}, 'robert': {'qw': '440.0'}, 'cv1': {'1': 100.5}, 'ROBERT': {'QW': 1560.0}, 'qwe': {'qw': '1555.0'}}

曆史購物資訊.txt

{'name': [['iphone', 5800],['bike', 800]], 'cx':[['iphone', 5800],['apple', 5],['apple', 5], ['book', 75]]}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.