Day10 作业
prize_dict = {
'0': "芭比娃娃",
'1': "变形金刚",
'2': "psp游戏机",
'3': "奥特曼",
'4': "遥控飞机",
'5': "chongqiwawa",
}
# 注册
def register():
user_name = input("用户名:\n").strip()
passwd_1 = input("密码:\n").strip()
passwd_2 = input("再次输入密码:\n").strip()
if passwd_1 == passwd_2:
with open("user_info.txt", "a") as fa:
fa.write(f"{user_name}:{passwd_1}\n")
print("注册成功")
else:
print("两次密码输入不一致")
def login():
count = 0
while count < 3:
user_name = input("用户名:\n").strip()
passwd = input("密码:\n").strip()
with open("user_info.txt", "r") as fr:
for info in fr:
usr_info, passwd_info = info.strip().split(":")
if user_name == usr_info and passwd == passwd_info:
print('登陆成功')
return True
count += 1
# 猜年龄
def guess_age(age):
count = 0
while count < 3:
inp_age = input("请输入年龄:\n").strip()
if not inp_age.isdigit():
print("请输入整数")
continue
inp_age = int(inp_age)
if inp_age == age:
print("猜对了")
return True
elif inp_age > age:
print("猜大了")
else:
print("猜小了")
count += 1
# 给奖品
def choose_prize():
prize_lst = []
while len(prize_lst) < 2:
print(prize_dict)
inp = input("请选择奖品:\n")
if inp in prize_dict:
prize = prize_dict[inp]
prize_lst.append(prize)
print(f"选择了:{prize}")
else:
print("选项不在列表中,请重新选择")
print(f"选择的奖品有:{prize_lst}")
age = 26
inp = input("1:注册, 2:登陆\n").strip()
if inp == "1":
register()
elif inp == "2":
if not login():
print("登陆失败")
exit()
if not guess_age(age):
print("猜测年龄失败")
exit()
choose_prize()
else:
print("输入错误,请重新输入")