# -*-coding:utf-8-*-
# Author: yqliu
# CreatDate: 2019/9/19 16:56
"""
在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如
2. 注册函数
3. 登录函数
4. 猜年龄函数
5. 选择奖品函数
流程图:1注册成功-》进行登陆
2重复注册--》重新注册3次
3.给三次机会进行登陆,3次登陆失败跳出
4给3次猜年龄,3次猜错询问时否继续猜测,如果3次猜错,不继续,退出游戏
5年龄猜对进行奖品选择,如果选择‘n',直接退出,否则打印选择的奖品列表
"""
import random
age=random.randint(1,100)
print (f"随机产生的年龄为{age}")
prize_dict={
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
'6': "再来一次",
'7': "欢迎下次光临",
}
def register():
print ("欢迎使用注册函数")
count = 0
while count < 3:
username = input("注册用户名:").strip()
passwd = input("请输入密码:").strip()
re_passwd = input("请再次输入密码:").strip()
if passwd == re_passwd:
user_info = f"{username}:{passwd}"
with open("user_info.txt", 'r', encoding='utf-8') as fr, open('user_info.txt', 'a', encoding='utf-8') as fw:
data = fr.read().split()
print(data)
if user_info in data:
print(f"{username}已经注册,你还可以尝试{2-count}次")
count += 1
continue
else:
fw.write(f"{username}:{passwd}\n")
print(f"{username}注册成功")
return True
else:
print(f"两次密码不一致,你还可以输入{2-count}次")
count += 1
def login():
print ("欢迎使用登陆函数")
count = 0
while count < 3:
usename = input("请输入用户名:").strip()
passwd = input("请输入密码:").strip()
user_info = f"{usename}:{passwd}"
with open("user_info.txt", 'r', encoding='utf-8') as fr:
data = fr.read().split()
if user_info in data:
print(f"{usename}登陆成功")
return True
else:
print(f"{usename}用户名或者密码输入有误,你还可以输入{2-count}次")
count += 1
def guess():
print ("欢迎进行猜年龄游戏")
count = 0
while count < 3:
age_inp = input("请用户输入猜的年龄:").strip()
if not age_inp.isdigit():
print("请输入数字字符,其他字符无效")
age_inp = int(age_inp)
if age_inp > age:
print("猜大了")
elif age_inp < age:
print("猜小了")
else:
print("猜对了")
prize_choice()
TAG=False
break
count += 1
if count!=3:
continue
again_choice=input("请选择是否继续游戏,y和Y继续,其他任意键退出")
if again_choice=='y' or again_choice=='Y':
count=0
def prize_choice():
print ("欢迎进行奖品选择")
print(prize_dict)
prize_user_dict = {}
for i in range(2):
user_choice = input("请输入序列号选择奖品,输入n和N退出")
if not (user_choice == 'n' or user_choice == 'N'):
print(f"用户选择的奖品为{prize_dict[user_choice]}")
prize = prize_dict[user_choice]
if prize not in prize_user_dict:
prize_user_dict[prize] = 1
else:
prize_user_dict[prize] += 1
else:
print("欢迎下次再来")
break
print(f"用户最终选择的奖品列表为{prize_user_dict}")
TAG=True
while TAG:
if not register():
print ("注册成功")
break
if not login():
break
else:
guess()
break
原文:https://www.cnblogs.com/ztzdhbg/p/11551585.html