真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Python基于文件操作實現(xiàn)購物車的示例

這篇“Python基于文件操作實現(xiàn)購物車的示例”文章,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要參考一下,對于“Python基于文件操作實現(xiàn)購物車的示例”,小編整理了以下知識點(diǎn),請大家跟著小編的步伐一步一步的慢慢理解,接下來就讓我們進(jìn)入主題吧。

石獅ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

python可以做什么

Python是一種編程語言,內(nèi)置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門的大數(shù)據(jù)分析,人工智能,Web開發(fā)等。

Python 基于文件操作實現(xiàn)購物車

學(xué)習(xí)Python有一段時間了,想著需要找些東西寫一下來鞏固下基礎(chǔ),看到了購物車然后想著能不能利用已經(jīng)學(xué)過的實現(xiàn)以下功能。但是比較簡單的實現(xiàn)也沒啥意義,只用幾個循環(huán)和判斷寫出來也沒用到些啥于是想著能不能更進(jìn)一步修改一下,做一個優(yōu)化。剛好學(xué)到的文件操作可以存儲一些信息。于是,優(yōu)化的想法就有了,廢話不多說,上代碼。

# coding:utf-8# author:w_uimport time# 獲取當(dāng)前時間函數(shù),用于顯示交易時間以及當(dāng)前時間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("現(xiàn)在的時間是:\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))
                            # 判斷輸入工資是否是純數(shù)字,因為工資不可能是字母
                            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函數(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)在的工資為\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("老板買點(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("購買\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))
                                                # 購買商品的花費(fèi),需要更新購買后用戶的工資
                                                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("您需要繼續(xù)購買嗎?\n 1.繼續(xù)購物\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:
                # 這里設(shè)置商家是一個管理員的模式,所以商家不用注冊直接登陸查看
                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 + "歡迎進(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("是否需要添加商品:\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("輸入錯誤!")

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

以上是“Python基于文件操作實現(xiàn)購物車的示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


名稱欄目:Python基于文件操作實現(xiàn)購物車的示例
轉(zhuǎn)載來源:http://weahome.cn/article/jsjgps.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部