這篇“Python基于文件操作實現購物車的示例”文章,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要參考一下,對于“Python基于文件操作實現購物車的示例”,小編整理了以下知識點,請大家跟著小編的步伐一步一步的慢慢理解,接下來就讓我們進入主題吧。
Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
Python 基于文件操作實現購物車
學習Python有一段時間了,想著需要找些東西寫一下來鞏固下基礎,看到了購物車然后想著能不能利用已經學過的實現以下功能。但是比較簡單的實現也沒啥意義,只用幾個循環和判斷寫出來也沒用到些啥于是想著能不能更進一步修改一下,做一個優化。剛好學到的文件操作可以存儲一些信息。于是,優化的想法就有了,廢話不多說,上代碼。
# 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("現在的時間是:\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("輸入錯誤!")寫到這,實現基本的功能還是沒有問題的,可以將用戶信息、商家信息等等等等存入文件里,下次再需要使用的時候直接從文件里調用出來,就不用像平常的運行一遍輸入一遍啦,用戶的工資也是可以保存的,商家可以像貨架上添加商品。商家是作為管理員的角色,所以初始的賬號密碼是固定存在一個文件里。本來想添加一個修改商家信息,但是想想還是一樣的操作,就直接省了這一步。
以上是“Python基于文件操作實現購物車的示例”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。