username_list = []
# flag = 0
prize_dict = {'0': 'durex', '1': 'okamoto', '2': 'Jissbon'}
shopping_car_dict = {}
def register():
print('欢迎来到注册功能:')
username_register, userpwd_register = user_inp_func()
with open('user_info.txt', 'a', encoding='utf8') as fa:
fa.write(f'{username_register}:{userpwd_register}\n')
print(f'用户{username_register}注册成功')
def login():
if username_list:
print('请勿重复登陆.')
# if not judgement():
# print('请勿重复登陆.')
login_count = 0
while login_count < 3:
username_inp, userpwd_inp = user_inp_func()
with open('user_info.txt', 'r', encoding='utf8') as fr:
for user_info in fr:
user_info = user_info.strip()
username_login, userpwd_login = user_info.split(':')
if username_login == username_inp and userpwd_login == userpwd_inp:
print('登录成功')
# global flag
# flag = 1
login_count = 3
username_list.append(username_login)
break
else:
print('账号密码错误')
login_count += 1
def logout():
# if not flag :
# print('请先登录.')
# return
if not username_list:
print('请先登录.')
return
username_list.clear()
print('注销成功.')
def shopping():
if not username_list:
print('请先登录.')
return
print('欢迎选购:')
print(prize_dict)
prize_choice = input('请输入商品代码:')
prize_name = prize_dict[prize_choice]
if not prize_choice in prize_dict:
print('无效商品代码.')
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():
if not username_list:
print('请先登录.')
return
print(f'恭喜购物成功:{shopping_car_dict}')
shopping_car_dict.clear()
def user_inp_func():
username_inp = input('请输入用户名:')
userpwd_inp = input('请输入密码:')
return username_inp, userpwd_inp
# def judgement():
# if username_list:
# print('请先登录后使用..')
# return
# else:
# return 1
func_dict = {
'1': register,
'2': login,
'3': logout,
'4': shopping,
'5': shopping_car,
}
def start():
while 1:
print('''
1 注册
2 登录
3 注销
4 购物
5 购物车
q 退出
''')
user_choice = input('请输入功能:(输入q退出)')
if user_choice == 'q':
break
if user_choice not in func_dict:
print('请输入正确功能键:')
continue
func_dict[user_choice]()
start()