資訊內(nèi)容
講解Python 基于文件操作實(shí)現(xiàn)購(gòu)物車

免費(fèi)學(xué)習(xí)推薦:python視頻教程W9D少兒編程網(wǎng)-https://www.pxcodes.com
Python 基于文件操作實(shí)現(xiàn)購(gòu)物車W9D少兒編程網(wǎng)-https://www.pxcodes.com
學(xué)習(xí)Python有一段時(shí)間了,想著需要找些東西寫(xiě)一下來(lái)鞏固下基礎(chǔ),看到了購(gòu)物車然后想著能不能利用已經(jīng)學(xué)過(guò)的實(shí)現(xiàn)以下功能。但是比較簡(jiǎn)單的實(shí)現(xiàn)也沒(méi)啥意義,只用幾個(gè)循環(huán)和判斷寫(xiě)出來(lái)也沒(méi)用到些啥于是想著能不能更進(jìn)一步修改一下,做一個(gè)優(yōu)化。剛好學(xué)到的文件操作可以存儲(chǔ)一些信息。于是,優(yōu)化的想法就有了,廢話不多說(shuō),上代碼。W9D少兒編程網(wǎng)-https://www.pxcodes.com
# coding:utf-8# author:w_uimport time# 獲取當(dāng)前時(shí)間函數(shù),用于顯示交易時(shí)間以及當(dāng)前時(shí)間def get_time(): now_time = time.strftime("%y-%m-%d %H:%M:%S") return now_time# 定義好各個(gè)文件操作需要用到的中間媒介user_information = {} user_salary = {} admin_information = {}shopping_list = []add_product = []shopping_car = []print("*" * 25 + "歡迎光臨".center(0) + "*" * 25)while True: print("現(xiàn)在的時(shí)間是:33[32;1m%s33[0m" % get_time()) print("您是用戶或者是商家: 1.用戶 2.商家") while True: user_choose1 = input(">>>:") # 由于用戶輸入并不可靠,所以這里判斷一下用戶輸入信息!以下皆是如此 if user_choose1.isdigit(): user_choose1 = int(user_choose1) if user_choose1 == 1: while True: print("請(qǐng)選擇注冊(cè)、登錄或者退出: 1.注冊(cè) 2.登錄 3.退出") user_choose2 = input(">>>") if user_choose2.isdigit(): user_choose2 = int(user_choose2) if user_choose2 == 1: username = input("請(qǐng)輸入用戶名:") password = input("請(qǐng)輸入密碼:") user_information[username] = password # 將用戶注冊(cè)信息存放到字典并以字符串形式存放到文件里,因?yàn)閷?xiě)模式會(huì)把原信息覆蓋所以這里選擇使用追加方式打開(kāi)文件 with open("user_information", 'a+', encoding="utf-8") as f: f.write(str(user_information)) # 判斷輸入工資是否是純數(shù)字,因?yàn)楣べY不可能是字母 while True: salary = input("請(qǐng)輸入工資:") if salary.isdigit(): salary = int(salary) user_salary[username] = salary # 將用戶輸入的工資綁定到對(duì)印度個(gè)用戶名上,用于登錄查看用戶工資 with open("user_salary", 'a+', encoding="utf-8") as f: f.write(str(user_salary)) break else: print("非法字符!請(qǐng)重新輸入!") elif user_choose2 == 2: username_input = input("請(qǐng)輸入用戶名:") password_input = input("請(qǐng)輸入密碼:") with open("user_information", 'r+', encoding="utf-8") as f: data = f.read() # 使用eval函數(shù)將文件讀取的字符串形式轉(zhuǎn)換為為字典 user_information = eval(data) if user_information[username_input] == password_input: print("登陸成功!") print("*" * 25 + "歡迎光臨本店".center(0) + "*" * 25) # 獲取用戶工資 with open("user_salary", 'r+', encoding="utf-8") as f: data1 = f.read() user_salary = eval(data1) print(f"你現(xiàn)在的工資為33[32;1m{user_salary[username_input]}33[0m") # 獲取購(gòu)物車的信息并打印 with open("shopping_list", 'r+', encoding="utf-8") as f: data2 = f.read() shopping_list = eval(data2) while True: for item in enumerate(shopping_list): print(item) user_choose3 = input("老板買點(diǎn)啥:") if user_choose3.isdigit(): user_choose3 = int(user_choose3) for i in range(0, len(shopping_list) + 1): if user_choose3 == i: shopping_car.append(shopping_list[user_choose3][0]) shopping_time = get_time() print("購(gòu)買33[32;1m %s33[0m* 1" % shopping_list[user_choose3][0]) print("交易時(shí)間:33[32;1m %s33[0m* 1" % shopping_time) # 將用戶購(gòu)買的物品存入到購(gòu)物車文件里,并且記錄交易時(shí)間 with open("shopping_car", 'a+', encoding="utf-8") as f: f.write(str(shopping_car)) f.write(str(shopping_time)) # 購(gòu)買商品的花費(fèi),需要更新購(gòu)買后用戶的工資 if user_salary[username_input] >= int(shopping_list[user_choose3][1]): user_salary[username_input] = user_salary[username_input] - int( shopping_list[user_choose3][1]) print(f"剩余工資:33[33;1m{user_salary[username_input]}33[0m") # 購(gòu)買后用戶所剩下的工資重新寫(xiě)入到文件里 with open("user_salary", 'r+', encoding="utf-8") as f: f.write(str(user_salary)) while True: user_choose4 = input("您需要繼續(xù)購(gòu)買嗎? 1.繼續(xù)購(gòu)物 2.退出 ") if user_choose4.isdigit(): user_choose4 = int(user_choose4) if user_choose4 == 1: break else: print("*" * 25 + "購(gòu)物車".center(0) + "*" * 25) print(shopping_car) print( f"剩余工資:33[33;1m{user_salary[username_input]}33[0m") exit() else: print("該用戶不存在!") elif user_choose2 == 3: exit() else: print("輸入錯(cuò)誤,請(qǐng)重新輸入!") elif user_choose1 == 2: # 這里設(shè)置商家是一個(gè)管理員的模式,所以商家不用注冊(cè)直接登陸查看 print("請(qǐng)先登錄:") admin_input = input("請(qǐng)輸入用戶名:") admin_password_input = input("請(qǐng)輸入密碼:") with open("admin_information", 'r+', encoding="utf-8") as f: data = f.read() admin_information = eval(data) # 校驗(yàn)信息 if admin_information[admin_input] == admin_password_input: print("*" * 25 + "歡迎進(jìn)入管理系統(tǒng)".center(0) + "*" * 25) print("以下是現(xiàn)貨架上商品有") with open("shopping_list", 'r+', encoding="utf-8") as f: data2 = f.read() shopping_list = eval(data2) for item in enumerate(shopping_list): print(item) while True: admin_choose = input("是否需要添加商品: 1.添加商品 2. 退出 >>>:") if admin_choose.isdigit(): admin_choose = int(admin_choose) if admin_choose == 1: add_product_name = input("請(qǐng)輸入商品名:") add_product_price = input("請(qǐng)輸入價(jià)格:") add_product.append(add_product_name) add_product.append(add_product_price) shopping_list.append(add_product) with open("shopping_list", 'r+', encoding="utf-8") as f: f.write(str(shopping_list)) elif admin_choose == 2: print("感謝使用!") exit() else: print("輸入錯(cuò)誤") else: print("輸入錯(cuò)誤!")寫(xiě)到這,實(shí)現(xiàn)基本的功能還是沒(méi)有問(wèn)題的,可以將用戶信息、商家信息等等等等存入文件里,下次再需要使用的時(shí)候直接從文件里調(diào)用出來(lái),就不用像平常的運(yùn)行一遍輸入一遍啦,用戶的工資也是可以保存的,商家可以像貨架上添加商品。商家是作為管理員的角色,所以初始的賬號(hào)密碼是固定存在一個(gè)文件里。本來(lái)想添加一個(gè)修改商家信息,但是想想還是一樣的操作,就直接省了這一步。W9D少兒編程網(wǎng)-https://www.pxcodes.com
大量免費(fèi)學(xué)習(xí)推薦,敬請(qǐng)?jiān)L問(wèn)python教程(視頻)W9D少兒編程網(wǎng)-https://www.pxcodes.com
以上就是講解Python 基于文件操作實(shí)現(xiàn)購(gòu)物車的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注少兒編程網(wǎng)其它相關(guān)文章!W9D少兒編程網(wǎng)-https://www.pxcodes.com

- 上一篇
python基礎(chǔ)知識(shí)之二:網(wǎng)絡(luò)通信數(shù)據(jù)傳輸
簡(jiǎn)介python實(shí)現(xiàn)網(wǎng)絡(luò)通信數(shù)據(jù)傳輸(基礎(chǔ)知識(shí)(二))免費(fèi)學(xué)習(xí)推薦:python視頻教程前言基礎(chǔ)知識(shí)學(xué)習(xí)一、OSI模型二、TCP、IP協(xié)議族:三、python中字符串的編碼方式結(jié)束語(yǔ)前言第二彈來(lái)了!今天還是上些基礎(chǔ)知識(shí)吧,扎實(shí)的基礎(chǔ)才是深入學(xué)習(xí)的不二法門?。?!基礎(chǔ)知識(shí)學(xué)習(xí)一、OSI模型OSI模型把網(wǎng)絡(luò)通
- 下一篇
python如何終止線程前言 · 零解決方案 · 壹解決方案 · 貳解決方案 · 叁
簡(jiǎn)介python終止線程的方法:1、調(diào)用stop函數(shù),并使用join函數(shù)來(lái)等待線程合適地退出;2、在python線程里面raise一個(gè)Exception;3、用“thread.join”方式結(jié)束線程。本文操作環(huán)境:windows7系統(tǒng)、python3.5版,DELLG3電腦。前言·零我們知道,在pyth