猜测年龄小游戏作业
def register():
"""注册函数"""
count = 0
while count < 3:
inp_name = (input('请输入注册姓名')).strip()
inp_pwd = (input('请输入密码')).strip()
inp_pwd_re =(input('请再次确认密码')).strip()
if not inp_pwd == inp_pwd_re:
print('输入的两次密码不相匹配,请重新输入')
count += 1
continue
with open('注册信息.txt', 'a', encoding='utf8') as fa:
fa.write(f'{inp_name}:{inp_pwd}
')
fa.flush()
break
def login():
"""登录函数"""
count = 0
while count < 3 :
inp_name = input('请输入用户名')
inp_pwd = input('请输入密码')
with open('注册信息.txt','r',encoding='utf8') as fileR:
for i in fileR:
username , userpwd = i.strip().split(':')
if inp_name == username and inp_pwd == userpwd :
print('登录成功')
count = 3
break
else:
print('输入的用户名或密码不匹配请重新输入')
count += 1
def gust_age():
"""猜测年龄"""
count = 0
age = 25
while count < 3:
inp_age = input('请输入猜测年龄')
if not inp_age.isdigit():
print('沙雕你输入的是数字吗?请重新输入,你只有三次沙雕机会')
count += 1
continue
int_age = int(inp_age)
if int_age < age :
print('替你智商着急,猜小了')
count += 1
elif int_age > age :
print('替你智商着急,猜大了')
count += 1
elif count > 0 and int_age == age :
print('终于猜对了沙雕,你可以选择奖品了')
chose_prize()
break
else:
print('恭喜猜对了,选择奖品')
chose_prize()
break
def chose_prize():
"""选择商品"""
prize_list = {
'1':'老男孩视频课程',
'2':'老男孩线下体验学习两个月',
'3':'老婆一个',
'4':'月薪两万工作一份',
'5':'充气娃娃一套',
'6':'苹果Pro一台',
'7':'Iphone手机一台'
}
str_list ="""
'1':'老男孩视频课程',
'2':'老男孩线下体验学习两个月',
'3':'老婆一个',
'4':'月薪两万工作一份',
'5':'充气娃娃一套',
'6':'苹果Pro一台',
'7':'Iphone手机一台'
"""
count = 0
chose_prize={}
while count < 2:
print(str_list)
inp_chose = input('输入你选择的奖品序号')
goods = prize_list[inp_chose]
if not inp_chose.isdigit():
print('请输入正确的选择数字序号')
if inp_chose in prize_list.keys():
print(f'恭喜获得,{prize_list.get(inp_chose)}')
count += 1
if goods in chose_prize:
chose_prize[goods] += 1
print(f'恭喜获得,{goods}奖品数{chose_prize.get(goods)}')
else:
chose_prize[goods] = 1
print(chose_prize)
def main():
count = 0
while count <3 :
Haveuser = input("是否注册过用户,请输入'是'或'否’")
if Haveuser =='是':
login()
gust_age()
break
elif Haveuser =='否':
register()
else:
print('请正确输入选择')
main()