'''
在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
1.登录函数
2.注册函数
3.猜年龄函数,可猜三次,三次猜错退出
4.选择奖品函数,可选两次,不想要选n/N,选完退出
'''
import random
p_dc = {'1':'篮球','2':'足球','3':'鞋子'}
ch_dc = {}
#1.注册
def register():
'''注册函数'''
count1 = 1
while count1 < 4:
ip_na = input('请输入注册名:')
ip_pw1 = input('请输入注册密码')
ip_pw2 = input('请确认密码:')
if not ip_pw1 == ip_pw2:
print('两次密码输入不一致,请重试')
count1 += 1
continue
with open('ip_if.txt', 'a') as fw:
fw.write(f'{ip_na}:{ip_pw1}\n')
break
else:
print('操作次数过于频繁')
# register()
#2.登录函数
def login():
count2 = 1
while count2 < 4:
ip_na = input('请输入登录名:')
ip_pw = input('请输入登录密码:')
with open('ip_if.txt', 'r') as fr:
for i in fr:
if not (ip_na + ':' + ip_pw) == i.strip():
continue
print('登录成功')
return
else:
print('用户名或密码错误,请重试')
count2 += 1
# login()
#3.猜年龄
def guess():
count3 = 1
while count3 < 4:
t_a = 18
i_a = int(input('请输入你猜的年龄'))
if i_a > t_a:
print(f'猜大了,你还有{3-count3}次机会')
count3 += 1
elif i_a < t_a:
print(f'猜小了,你还有{3-count3}次机会')
count3 += 1
else:
print('恭喜你猜对了,请选择你的奖品')
select()
break
# guess()
#4.选择奖品
def select():
count4 = 1
print('奖品列表如下')
for i, j in p_dc.items():
print(i, j)
while count4 < 3:
ch = input('请根据序号选择相应的奖品,输入其他则默认不要本次奖品:')
if not ch in p_dc:
count4 += 1
continue
if p_dc[ch] in ch_dc:
ch_dc[p_dc[ch]] += 1
print(f'恭喜获得1个{p_dc[ch]}')
else:
ch_dc[p_dc[ch]] = 1
print(f'恭喜获得1个{p_dc[ch]}')
count4 += 1
print('总奖品情况如下:')
for i, j in ch_dc.items():
print(f'{j}个{i}', end=' ')
func_dic = {'a':register,'b':login,'c':guess,}
print('''欢迎,功能如下:
a 注册
b 登录
c 开始游戏
d 退出
请根据字母选择
''')
count5 = 1
while count5 < 6:
if count5 == 5:
print('操作过于频繁程序退出')
break
choice = input('请选择:')
if choice == 'd':
break
if not choice in func_dic:
print('非法输入,请重试')
count5 += 1
continue
func_dic[choice]()
count5 += 1
if choice == 'c':
break
转载于:https://www.cnblogs.com/-406454833/p/11559091.html