1.在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
- 登录函数
- 注册函数
- 猜年龄函数
- 选择奖品函数
age = 18 # 给定年龄
# 奖品信息
prize_dict = {
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
}
prize_msg = '''
0 芭比娃娃
1 变形金刚
2 psp游戏机
3 奥特曼
4 遥控飞机
5 chongqiwawa
'''
# 注册游戏
def register():
"""注册功能"""
print('欢迎注册游戏')
while True:
username_inp = input('请输入你注册的用户名:')
pwd_inp = input('请输入你的密码:')
re_pwd_inp = input('请再次输入你的密码:')
if not pwd_inp == re_pwd_inp:
print('两次密码输入不一致')
count += 1
continue
with open('user_info.txt', 'a', encoding='utf8') as fa:
fa.write(f'{username_inp}:{pwd_inp}\n')
fa.flush()
break
# 登录游戏
def login():
"""登录功能"""
print('欢迎登录游戏')
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.strip() == username_inp and pwd.strip() == pwd_inp:
print('登录成功')
break
else:
print('登录失败')
#猜年龄游戏
def game():
"""游戏判断功能"""
age_count = 0 # 计算次数
while age_count < 3:
age_inp = input('请输入你的年龄:')
if not age_inp.isdigit():
print(f'格式错误,请重新输入')
continue
age_inp_int = int(age_inp)
if age_inp_int > age:
print('猜大了')
elif age_inp_int < age:
print('猜小了')
else:
print('猜对了')
prize()
age_count += 1 # 失败计数加1
#领取游戏奖品
def prize():
"""领取奖品功能"""
get_prize_dict = {} # 获取的奖品信息
prize_count = 0
while prize_count < 2:
print(f'奖品如下:{prize_msg}')
# 与用户交互
prize_choice = input('请选择你需要的奖品:')
prize = prize_dict[prize_choice]
# 奖品信息放入购物车
if prize in get_prize_dict:
get_prize_dict[prize] += 1
else:
get_prize_dict[prize] = 1
print(f'恭喜你获得奖品 {prize}')
prize_count += 1
print(f'总共获得奖品为:{get_prize_dict}')
break
#调用
register()
login()
game()
prize()