标签:需求 提示 pen count == lock encoding 其他 nbsp
编写登陆接口
基础需求:
dic={
‘user1‘:{‘password‘:‘123‘,‘count‘:0},
‘user2‘:{‘password‘:‘123‘,‘count‘:0},
‘user3‘:{‘password‘:‘123‘,‘count‘:0}
}
count=0
while True:
username = input(‘请输入用户名:‘)
if username not in dic:
print("用户名不存在")
continue
if dic[username][‘count‘] >=3:
print("密码尝试次数过多,用户名已锁定,请确认用户名密码是否正确或者用其他用户登录")
continue
userpass = input("请输入密码 :")
if userpass == dic[username]["password"]:
print("登陆成功")
break
elif userpass !=dic[username]["password"]:
print("密码错误,请重新输入")
dic[username][‘count‘] += 1
continue
升级需求:
dic={‘user1‘:{‘passwd‘:‘123‘,‘count‘:0},
‘user2‘:{‘passwd‘:‘123‘,‘count‘:0},
‘user3‘:{‘passwd‘:‘123‘,‘count‘:0},
‘user4‘:{‘passwd‘:‘123‘,‘count‘:0}
}
count=0
while True:
username=input(‘请入用户名 :‘)
if username not in dic:
print("用户 %s 不存在" %(username))
continue
with open(‘user.txt‘, ‘r‘, encoding=‘utf-8‘) as f:
lock_user = (f.read().split(‘|‘))
if username in lock_user:
print("用户 %s 已锁定" %(username))
break
if dic[username][‘count‘] >= 3:
with open(‘user.txt‘,‘a‘) as f:
f.write(username+‘|‘)
print("用户 %s 已锁定" % (username))
break
password=input("请输入密码 ")
if password==dic[username][‘passwd‘]:
print("登陆成功 %s 欢迎回来" %username)
break
if password !=dic[username][‘passwd‘]:
print(‘密码错误,请重新输入‘)
dic[username][‘count‘]+=1
continue
猜年龄游戏升级版
要求:
correct_answer=30
count=0
input_list=[‘Y‘,‘y‘,‘N‘,‘n‘]
while True:
guess_age=int(input(‘请输入年龄: ‘))
if guess_age==correct_answer:
print("恭喜你,答对了")
break
else:
count+=1
if count > 2:
confirm = input("请输入Y或者y继续,输入N或者n退出(输入其他值,游戏结束):")
if confirm not in input_list:
break
if confirm==‘y‘ or confirm==‘Y‘:
count=0
continue
elif confirm==‘n‘ or confirm==‘N‘:
break
标签:需求 提示 pen count == lock encoding 其他 nbsp
原文地址:http://www.cnblogs.com/dbkeeper/p/7129247.html