import os
import random
user_info = {
'name': None,
'password': None
}
login_dict = {'login': None}
prize_dict ={
'1': '女朋友',
'2': 'ps4pro',
'3': 's9世界赛决赛门票',
'4': '霍格沃兹入学通知书',
'5': '哆啦A梦',
'6': '皮卡丘',
'7': '核弹发射按钮',
'8': '潘多拉魔盒',
'9': '死亡笔记',
'10': '幻想乡永久居住证'
}

def save(user_dict):
DB_PATH = os.path.dirname(os.path.dirname(os.path.abspath(file)))

user_path = os.path.join(DB_PATH, user_dict['name'])
with open(user_path, 'w', encoding='utf-8') as fw:
    user_dict = str(user_dict)
    fw.write(user_dict)
    fw.flush()

def select(name):
DB_PATH = os.path.dirname(os.path.dirname(os.path.abspath(file)))
user_path = os.path.join(DB_PATH, name)
if os.path.exists(user_path):
with open(user_path, 'r', encoding='utf-8') as fr:
return eval(fr.read())
return False

def register():
print('注册')
a = 0
while a < 3:
name = input('请输入姓名:').strip()
pwd = input('请输入密码:').strip()
re_pwd = input('请确认密码:').strip()

    if pwd != re_pwd:
        print('两次密码输入不一致')
        a += 1
        continue

    res = select(name)
    if res:
        print('用户已注册!')
        break

    user_info['name'] = name
    user_info['password'] = pwd
    save(user_info)
    print('注册成功')
    break

def login():
print('登录')
a = 0
while a < 3:
name = input('请输入姓名:').strip()
pwd = input('请输入密码:').strip()

    user_dict = select(name)
    if not user_dict:
        print('请先注册')
        register()

    if pwd != user_dict['password']:
        print('密码输入错误')
        a += 1
        continue

    login_dict['login'] = name
    print('登录成功')
    break

def guess():
if not login_dict['login']:
print('请先登录')
login()
age = random.randint(15, 20)
chance = 5
while chance > 0:
guess = input('请输入猜测的年龄:').strip()
if not guess.isdigit():
print('请输入数字')
continue
guess = int(guess)
if guess > age:
chance -= 1
print(f'猜大了,你还有{chance}次机会。')
continue
if guess < age:
chance -= 1
print(f'猜小了,你还有{chance}次机会。')
continue
print('恭喜你,猜对了!来选择两个奖品吧!')
while True:
print(prize_dict)
choice1 = input('请选择第一件奖品编号').strip()
if choice1 not in prize_dict:
print('请输入正确的编号')
continue
prize1 = prize_dict[choice1]
print(f'你的第一件奖品就是{prize1}啦')
prize_dict.pop(choice1)
break
while True:
print(prize_dict)
choice2 = input('请选择第二件奖品编号').strip()
if choice2 not in prize_dict:
print('请输入正确的编号')
continue
prize2 = prize_dict[choice2]
print(f'你的第二件奖品就是{prize2}啦')
break

def run():
register()
login()
guess()

run()

02-01 09:29