重構優化Python購物車

# -*- coding:utf-8 -*-

def regiter():
    while True:
        username = input("請輸入您的帳號:").strip()
        password = input("請輸入您的密碼:").strip()#去除空格及換號符
        with open('register',encoding='UTF-8')as f1:
            for line in f1:#循環讀取註冊文件中的內容
                line_list=line.strip().replace(',',',').split(',')#去除空格及換號符以及把中文逗號換成英文逗號
                #print(type(line_list))
                if username==line_list[0]:
                    print("用戶名已經存在,請從新輸入")
                    break
            else:
                with open('register', encoding='UTF-8',mode='a')as f2:
                    f2.write('\n{},{}'.format(username,password))
                    print("恭喜你註冊成功%s"%username)
                    return True #結束函數

def login():
    i=0#計數器
    while i<3:#超過3次後,登錄失敗
        username = input("請輸入您的帳號:").strip()
        password = input("請輸入您的密碼:").strip()  # 去除空格及換號符
        with open('register',encoding='UTF-8')as f1:
            for line in f1:#循環讀取註冊文件中的內容
                line_list=line.strip().replace(',',',').split(',')#去除空格及換號符以及把中文逗號換成英文逗號
                #print(type(line_list))
                if username==line_list[0]and password==line_list[1]:
                    print("*******登錄成功*******")
                    return True
            else:
                print("帳戶或密碼輸入錯誤")
                i+=1
def shopping():
    list_he=[]
    offer=input("請輸入您的儲值卡金額:").strip()
    if offer.isdigit():
        offer=int(offer)
        while True:
            shipin2 = [['牛奶', 20], ['肉乾', 30], ['大米', 15], ['麪包', 15], ['啤酒', 3.5]]
            for i, a in enumerate(shipin2, 1):  # 循環打印商品列表
                print("序號:%s" % i, "商品:%s" % a[0], "價格:%s元" % a[1])
            huo_qu = input("請輸入你要購買的商品,輸入[0]退出").strip()
            if huo_qu.isdigit():
                huo_qu=int(huo_qu)
                if huo_qu > 0 and huo_qu <= len(shipin2):  # 驗證輸入是否正確
                    j = shipin2[huo_qu - 1]  # 購買的商品和價格
                    if j[1] > offer:  # 判斷想要購買的商品價格是否超過了餘額
                        print("您的餘額不足,請及時充值")
                    else:
                        offer = offer - j[1]  # 算出購買商品後的價格
                        print("您購買的商品爲%s" % j[0], "剩餘金額爲%s" % offer)  # 輸出購買的商品
                        list_he.append(j[0])  # 把已購買商品添加至集合中
                        print("您已經購買了%s" % list_he)  # 已購買商品集合
                        continue
                elif huo_qu == 0:
                    print("退出程序,再見")
                    for m in list_he:
                        print("您購買了%s" % m)
                    break
                else:
                    print("商城貨物暫時短缺,請輸入正確的商品序號")
    else:
        print("您輸入的有非法字符,請從新輸入")
choice_dict={
    1:regiter,
    2:login,
    3:shopping
}
while True:
    print('''-------歡迎光臨購物商場-------
          1:註冊
          2:登錄
          3:購物
          4:退出
          ''')
    choice=input("請選擇序號").strip()
    if choice.isdigit():
        choice=int(choice)
        if choice>0 and choice<=len(choice_dict):
            choice_dict[choice]()
        else:
            print("您輸入的超出範圍")
    else:
        print("您輸入的有非法字符")
相關文章
相關標籤/搜索