在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
- 登录函数
- 注册函数
- 猜年龄函数
- 选择奖品函数
prize_dict={'0':'飞机','1':'坦克','2':'奥特曼','3':'手机'}
def register():
print('注册:》》》')
while True:
username = input('请输入用户名:')
pwd = input('请输入密码:')
re_pwd = input('请再次输入密码:')
if not pwd == re_pwd:
print('两次密码输入不一致,请重新输入》》')
continue
with open('user_info.txt','a',encoding='utf8')as fa:
fa.write(f'{username}:{pwd}
')
print('注册成功')
break
def login():
print('登录:》》》')
count=0
while count<3:
username_inp=input('请输入用户名:')
pwd_inp=input('请输入密码:')
with open('user_info.txt','r',encoding='utf8')as fr:
for user_info in fr:
username,pwd=user_info.split(':')
if username_inp==username.strip() and pwd_inp==pwd.strip():
print('登录成功')
game()
count=4
break
else:
print('账号密码错误,请重新登录')
count+=1
def game():
age=18
count=0
while count<3:
age_choice=input('请输入年龄:')
if not age_choice.isdigit():
print('非法输入')
continue
age_choice_int=int(age_choice)
if age_choice_int >age:
print('猜大了')
elif age_choice_int <age:
print('猜小了')
else:
print('猜对了')
prize()
break
count+=1
if count == 3:
again_choice = input("游戏结束,是否继续?Y/N :")
if again_choice in ['Y', 'y']:
count = 0
else:
break
def prize():
print(f'请选择奖品:》》
{prize_dict}')
prize_choice=input('选择奖品:')
print(f'恭喜获得奖品:{prize_dict[prize_choice]}')
func_msg='''
1: 注册
2: 登录
q: 退出
'''
func_dict={
'1': register,
'2': login,
}
while True:
print(func_msg)
choice = input('功能选择:')
if choice == 'q':
break
func_dict[choice]()