国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

講解Python 基于文件操作實現購物車

極客小將2021-03-01-
簡介免費學習推薦:python視頻教程Python基于文件操作實現購物車學習Python有一段時間了,想著需要找些東西寫一下來鞏固下基礎,看到了購物車然后想著能不能利用已經學過的實現以下功能。但是比較簡單的實現也沒啥意義,只用幾個循環和判斷寫出來也沒用到些啥于是想著能不能更進一步修改一下,做一個優化。剛
W9D少兒編程網-https://www.pxcodes.com

免費學習推薦:python視頻教程W9D少兒編程網-https://www.pxcodes.com

Python 基于文件操作實現購物車W9D少兒編程網-https://www.pxcodes.com

學習Python有一段時間了,想著需要找些東西寫一下來鞏固下基礎,看到了購物車然后想著能不能利用已經學過的實現以下功能。但是比較簡單的實現也沒啥意義,只用幾個循環和判斷寫出來也沒用到些啥于是想著能不能更進一步修改一下,做一個優化。剛好學到的文件操作可以存儲一些信息。于是,優化的想法就有了,廢話不多說,上代碼。W9D少兒編程網-https://www.pxcodes.com

# coding:utf-8# author:w_uimport time# 獲取當前時間函數,用于顯示交易時間以及當前時間def get_time(): now_time = time.strftime("%y-%m-%d %H:%M:%S") return now_time# 定義好各個文件操作需要用到的中間媒介user_information = {} user_salary = {} admin_information = {}shopping_list = []add_product = []shopping_car = []print("*" * 25 + "歡迎光臨".center(0) + "*" * 25)while True: print("現在的時間是: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("請選擇注冊、登錄或者退出: 1.注冊 2.登錄 3.退出") user_choose2 = input(">>>") if user_choose2.isdigit(): user_choose2 = int(user_choose2) if user_choose2 == 1: username = input("請輸入用戶名:") password = input("請輸入密碼:") user_information[username] = password # 將用戶注冊信息存放到字典并以字符串形式存放到文件里,因為寫模式會把原信息覆蓋所以這里選擇使用追加方式打開文件 with open("user_information", 'a+', encoding="utf-8") as f: f.write(str(user_information)) # 判斷輸入工資是否是純數字,因為工資不可能是字母 while True: salary = input("請輸入工資:") if salary.isdigit(): salary = int(salary) user_salary[username] = salary # 將用戶輸入的工資綁定到對印度個用戶名上,用于登錄查看用戶工資 with open("user_salary", 'a+', encoding="utf-8") as f: f.write(str(user_salary)) break else: print("非法字符!請重新輸入!") elif user_choose2 == 2: username_input = input("請輸入用戶名:") password_input = input("請輸入密碼:") with open("user_information", 'r+', encoding="utf-8") as f: data = f.read() # 使用eval函數將文件讀取的字符串形式轉換為為字典 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"你現在的工資為33[32;1m{user_salary[username_input]}33[0m") # 獲取購物車的信息并打印 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("老板買點啥:") 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("購買33[32;1m %s33[0m* 1" % shopping_list[user_choose3][0]) print("交易時間:33[32;1m %s33[0m* 1" % shopping_time) # 將用戶購買的物品存入到購物車文件里,并且記錄交易時間 with open("shopping_car", 'a+', encoding="utf-8") as f: f.write(str(shopping_car)) f.write(str(shopping_time)) # 購買商品的花費,需要更新購買后用戶的工資 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") # 購買后用戶所剩下的工資重新寫入到文件里 with open("user_salary", 'r+', encoding="utf-8") as f: f.write(str(user_salary)) while True: user_choose4 = input("您需要繼續購買嗎? 1.繼續購物 2.退出 ") if user_choose4.isdigit(): user_choose4 = int(user_choose4) if user_choose4 == 1: break else: print("*" * 25 + "購物車".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("輸入錯誤,請重新輸入!") elif user_choose1 == 2: # 這里設置商家是一個管理員的模式,所以商家不用注冊直接登陸查看 print("請先登錄:") admin_input = input("請輸入用戶名:") admin_password_input = input("請輸入密碼:") with open("admin_information", 'r+', encoding="utf-8") as f: data = f.read() admin_information = eval(data) # 校驗信息 if admin_information[admin_input] == admin_password_input: print("*" * 25 + "歡迎進入管理系統".center(0) + "*" * 25) print("以下是現貨架上商品有") 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("請輸入商品名:") add_product_price = input("請輸入價格:") 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("輸入錯誤") else: print("輸入錯誤!")

寫到這,實現基本的功能還是沒有問題的,可以將用戶信息、商家信息等等等等存入文件里,下次再需要使用的時候直接從文件里調用出來,就不用像平常的運行一遍輸入一遍啦,用戶的工資也是可以保存的,商家可以像貨架上添加商品。商家是作為管理員的角色,所以初始的賬號密碼是固定存在一個文件里。本來想添加一個修改商家信息,但是想想還是一樣的操作,就直接省了這一步。W9D少兒編程網-https://www.pxcodes.com

大量免費學習推薦,敬請訪問python教程(視頻)W9D少兒編程網-https://www.pxcodes.com

以上就是講解Python 基于文件操作實現購物車的詳細內容,更多請關注少兒編程網其它相關文章!W9D少兒編程網-https://www.pxcodes.com

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 诏安县| 桐乡市| 长乐市| 舟曲县| 东城区| 绥棱县| 关岭| 阿合奇县| 杨浦区| 五家渠市| 兰坪| 高要市| 莲花县| 阳西县| 兴安县| 当雄县| 东城区| 南宫市| 莱西市| 江阴市| 永和县| 云霄县| 内丘县| 衡水市| 岐山县| 郎溪县| 汾阳市| 双柏县| 巴林左旗| 扶风县| 博客| 张家港市| 岚皋县| 博野县| 北票市| 漳州市| 辽宁省| 邯郸市| 福安市| 平陆县| 临海市|