Python 基于檔案操作實作購物車
學習Python有一段時間了,想著需要找些東西寫一下來鞏固下基礎,看到了購物車然后想著能不能利用已經學過的實作以下功能,但是比較簡單的實作也沒啥意義,只用幾個回圈和判斷寫出來也沒用到些啥于是想著能不能更進一步修改一下,做一個優化,剛好學到的檔案操作可以存盤一些資訊,于是,優化的想法就有了,廢話不多說,上代碼,
# coding:utf-8
# author:w_u
import 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("現在的時間是:\033[32;1m%s\033[0m" % get_time())
print("您是用戶或者是商家:\n 1.用戶\n 2.商家")
while True:
user_choose1 = input(">>>:")
# 由于用戶輸入并不可靠,所以這里判斷一下用戶輸入資訊!以下皆是如此
if user_choose1.isdigit():
user_choose1 = int(user_choose1)
if user_choose1 == 1:
while True:
print("請選擇注冊、登錄或者退出:\n 1.注冊 \n 2.登錄 \n 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"你現在的工資為\033[32;1m{user_salary[username_input]}\033[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("購買\033[32;1m %s\033[0m* 1" % shopping_list[user_choose3][0])
print("交易時間:\033[32;1m %s\033[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"剩余工資:\033[33;1m{user_salary[username_input]}\033[0m")
# 購買后用戶所剩下的工資重新寫入到檔案里
with open("user_salary", 'r+', encoding="utf-8") as f:
f.write(str(user_salary))
while True:
user_choose4 = input("您需要繼續購買嗎?\n 1.繼續購物\n 2.退出\n")
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"剩余工資:\033[33;1m{user_salary[username_input]}\033[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("是否需要添加商品:\n 1.添加商品 \n 2. 退出 \n >>>:")
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("輸入錯誤!")
寫到這,實作基本的功能還是沒有問題的,可以將用戶資訊、商家資訊等等等等存入檔案里,下次再需要使用的時候直接從檔案里呼叫出來,就不用像平常的運行一遍輸入一遍啦,用戶的工資也是可以保存的,商家可以像貨架上添加商品,商家是作為管理員的角色,所以初始的賬號密碼是固定存在一個檔案里,本來想添加一個修改商家資訊,但是想想還是一樣的操作,就直接省了這一步,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264491.html
標籤:python
