目录
猜年龄游戏升级版
# 注册模块
def regist():
count = 0
while count < 3:
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()
print('恭喜你注册成功,赶快登录进行游戏吧')
break
# 登录模块
def login():
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 guess():
age = 18
age_count = 0
while age_count < 3:
age_inp = input('请输入你猜的年龄:')
age_count += 1
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('恭喜你猜对了,快去获取属于你的奖品吧')
break
# 奖品清单模块
def gift():
prize_dict = {
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
}
prize_msg = '''
0 芭比娃娃
1 变形金刚
2 psp游戏机
3 奥特曼
4 遥控飞机
5 chongqiwawa
'''
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}')
print('游戏结束,你可以选择退出游戏')
# 主程序
while True:
print('''
欢迎游玩猜年龄游戏!
1 登录
2 注册
3 开始游戏
4 获取奖品
5 退出游戏
''')
choice = input('>>: ').strip()
if choice == '1':
login()
elif choice == '2':
regist()
elif choice == '3':
guess()
elif choice == '4':
gift()
elif choice == '5':
break
else:
print('非法操作')