# 注册
def register():
print('欢迎来到注册功能')
while True:
username_inp = input('请输入用户名:')
pwd_inp = input('请输入密码:')
re_pwd_inp = input('请再次输入密码:')
if not username_inp.isalpha():
print('用户名应为纯字母')
continue
if not pwd_inp.isdigit():
print('密码应为纯数字')
continue
with open(r'user_info.txt', 'r', encoding='utf-8') as fr:
user_info = fr.read()
username = f'|{username_inp}:'
if username in user_info:
print('用户名已注册')
continue
if re_pwd_inp == pwd_inp:
with open('user_info.txt', 'a', encoding='utf-8') as fa:
fa.write(f'|{username_inp}:{pwd_inp}')
print('注册成功')
return True
print('两次密码不一致')
# 登录
def login():
print('欢迎来到登录功能')
while True:
username_inp = input('请输入应用户名:')
pwd_inp = input('请输入密码:')
user_info_inp = f'|{username_inp}:{pwd_inp}'
with open(r'user_info.txt', 'r', encoding='utf-8') as fr:
user_info = fr.read()
if user_info_inp in user_info:
print('登录成功')
return True
print('账号密码错误')
# 奖品机制
def jiang_pin():
prize_dict = {'0': '918', '1': 'p1', '2': 'laferrari', '3': 'chongqiwawa'}
prize_msg = '''
0 = 918
1 = p1
2 = laferrari
3 = chongqiwawa
'''
prize_d = {}
prize_count = 0
while prize_count < 2:
print('请从下列奖品中选择一个')
print(prize_msg)
prize_inp = input('请选择')
if prize_inp not in prize_dict:
print('请选择现有奖品')
continue
prize = prize_dict[prize_inp]
if prize in prize_d:
prize_d[prize] += 1
else:
prize_d[prize] = 1
print(f'你获得了{prize}')
prize_count += 1
print(f'你一共获得了{prize_d}')
print('游戏结束')
return None
# 猜年龄游戏
def cai():
print('欢迎来到猜年龄游戏,你只有三次机会')
age = 18
age_count = 0
while age_count < 3:
age_inp = input('请输入年龄:')
if not age_inp.isdigit():
print(f'你的年龄是{age_inp}')
continue
age_inp_int = int(age_inp)
if age_inp_int > age:
print('猜大了')
elif age_inp_int < age:
print('猜小了')
else:
print('你猜对了,可以选择两件奖品')
del_jianpin = jiang_pin()
if not del_jianpin:
break
age_count += 1
# 游戏主体
while True:
zhu_inp = input('先注册请输入y,enter直接进入登录')
if zhu_inp == 'y':
register = register()
if not register:
continue
login = login()
if not login:
continue
cai()
break