猜年龄函数版day10作业

age = 18
prize_dict = {0:'布娃娃',1:'变形金刚',2:'奥特曼',3:'<Python从入门到放弃>'}

def register():
'''注册功能'''
print('游戏注册')
while True:
username = input ('请输入你的用户名:')
pwd = input ('请输入你的密码:')
re_pwd = input('请确认你的密码:')
# 判断两次密码是否相同
if not pwd == re_pwd:
print('两次密码不一致,请重新输入 ')
continue
# 保存用户信息
with open('username_info.txt','a',encoding='utf8') as fa:
fa.write(f'{username}:{pwd} ')
print('注册成功')
break

def login():
'''登录功能'''
print('游戏登录')

username_inp = input('输入你的用户名:')
pwd_inp = input('输入你的密码:')

with open('username_info.txt','r',encoding='utf8') as fr:
    # 用户信息切分,比较
    for user_info in fr:
        username,pwd = user_info.split(':')
        if username.strip() == username_inp and pwd.strip() == pwd_inp:
            print('登录成功')
        else:
            print('登录失败')

def prize():
'''选择奖品'''
count = 0
while count < 2:
print(f'恭喜你猜对了,请选择你的奖品,奖品列表如下: {prize_dict}')
choice = input('请输入你想要的礼物编号,按q退出')
if choice != 'n':
print(f'恭喜你获得奖品:{prize_dict[int(choice)]}')
else:
break
count += 1

def game():
'''猜年龄游戏'''
count = 0
print('欢迎进入猜年龄游戏')
while count < 3:
age_inp = input('请输入年龄:')

    if not age_inp.isdigit():
        print('格式错误')
        continue
    int_age_inp = int(age_inp)
    if int_age_inp < age:
        print('猜小了')
    elif int_age_inp < age:
        print('猜大了')
    else:
        print('猜对了')
        prize()
        break
count += 1

register()
login()
game()

原文地址:https://www.cnblogs.com/shin09/p/11552806.html