username_list = [] prize_dict = { '0': "芭比娃娃", '1': "變形金剛", '2': "psp遊戲機", '3': "奧特曼", '4': "遙控飛機", '5': "chongqiwawa", } shopping_car_dict = {} def inp_username_pwd(): username_inp = input('請輸入你的用戶名:') pwd_inp = input('請輸入你的密碼:') return username_inp, pwd_inp def login(): print('歡迎來到登陸功能') if username_list: print('已經登陸,請勿重複登陸') return count = 0 while count < 3: username_inp, pwd_inp = inp_username_pwd() with open('user_info.txt', 'r', encoding='utf8') as fr: for user_info in fr: user_info = user_info.strip() username, pwd = user_info.split(':') if username == username_inp and pwd == pwd_inp: print('登陸成功') username_list.append(username_inp) return else: print('帳號密碼錯誤') count += 1 def register(): print('歡迎來到註冊功能') username_inp, pwd_inp = inp_username_pwd() with open('user_info.txt', 'a', encoding='utf8') as fa: fa.write(f'{username_inp}:{pwd_inp}\n') def logout(): print('歡迎來到註銷功能') if not username_list: print('請登陸後使用該功能') return username_list.clear() def shopping(): print('歡迎來到Nick集團消費功能') if not username_list: print('請登陸後使用該功能') return print(''' 0 芭比娃娃 1 變形金剛 2 psp遊戲機 3 奧特曼 4 遙控飛機 5 chongqiwawa ''') prize_choice = input('請輸入你須要購買的商品編號:') prize_name = prize_dict[prize_choice] if prize_name in shopping_car_dict: shopping_car_dict[prize_name] += 1 else: shopping_car_dict[prize_name] = 1 print(f'消費成功{prize_name},當前購物車狀況爲{shopping_car_dict}') def shopping_car(): print('恭喜剁手成功功能') if not username_list: print('請登陸後使用該功能') return print(f'恭喜你購物成功:{shopping_car_dict}') shopping_car_dict.clear() func_dict = { '1': login, '2': register, '3': logout, '4': shopping, '5': shopping_car, } while True: print(''' 1 登陸 2 註冊 3 註銷 4 購物 5 購物車 q 退出 ''') func_choice = input('請選擇你要選擇的功能(輸入q退出):') if func_choice == 'q': break if func_choice not in func_dict: print('傻逼,英文看不懂正常,還看不懂阿拉伯數字') continue func_dict[func_choice]()