您的位置:首页 > 编程语言 > Python开发

Python-S13作业-day1-之登陆程序

2016-06-23 15:15 429 查看

Python-S13-day1

需求:

1.让用户输入账号密码,账号密码正确,登陆程序,打印欢迎信息;

2.如果账号,或密码输入错误,提示用户重新输入,用户有三次机会;

3.如果用户第三次输入的账号或者密码还是不正确,就把第三次输入的账号锁定。

主程序:


#!/usr/bin/pythonenv
#_*_coding:utf-8_*_
userfile1='userfile'
lockfile1='lockfile'
userfile=open(userfile1,'r+')
lockfile=open(lockfile1,'r+')

login_flag=False
#设置一个标志位,用于判断用户是否登陆成功;
foriinrange(3):
username=input("username:")
forlockuserinlockfile.readlines():
lockuser=lockuser.split()
#把被锁定的账号,转换成一个列表;
ifusernameinlockuser:
#判断账号是否在锁定列表中,如果在,就退出;
exit("%suserislock"%username)
password=input("password:")
forlineinuserfile.readlines():
line=line.split()
#把允许登陆的用户名,密码转换成列表;
user=line[0]
#把登陆账号赋值给user
passwd=line[1]
#把登陆密码赋值给passwd
ifuser==usernameandpasswd==password:
#判断账号,密码是否正确,如果正确,登陆并打印欢迎信息;
exit("welcomtologin.")
login_flag=True
#标志位,判断用户是否登陆成功;
iflogin_flag==False:
print("userorpasswderror.")
else:
print("%suserislock."%username)
lockfile.write("\t%s"%username)
lockfile.close()
#循环三次后,用户还没有登陆成功,就把账号锁定;



Readme:

1.这个程序一共三个文件:userfile存放登陆账号,密码的文件;

            lockfile存放被锁定的账号;

            login.py登陆主程序;

流程图:



思路二(功能全部实现):

#!/usr/bin/pythonenv
#_*_coding:utf-8_*_
user=open('db','r+')
lock=open('db1','r+')
mark=False
foriinrange(3):
username=input("inputusername:")
forlineinlock.readlines():
line=line.split("|")
ifusernameinline:
exit("userislock.")
passwd=input("inputyoupassword.")
forpwdinuser.readlines():
pwd=pwd.split("|")
ifpwd[0]==usernameandpwd[1].strip("\n")==passwd:
exit("welcotologin.")
else:
print('userorpasswderror.')
else:
print("%suserislock"%username)
lockuser=open('db1','a')
lockuser.write("|"+"%s"%username)
lockuser.close()

*lockuser.txt
0||1|11

*loginuser.txt
admin|123
freddy|123



思路三(函数写法):

#用户输入三次,密码不正确,锁定账号,功能没有实现;

defregister(user,pwd):
reg=open('db','a')
reg.write("\n"+user+"|"+pwd)
reg.close()

deflogin(user,pwd):
lockuser=open('db1','r')
forlineinlockuser.readlines():
line=line.split("|")
ifuserinline:
exit("userislock.")
userpwd=open('db','r')
forlogpwdinuserpwd:
logpwd=logpwd.split("|")
ifuser==logpwd[0]andpwd==logpwd[1].strip("\n"):
exit("welcometologin")
else:
print("userorpassworderror")
input_num=input("inputyorn.")
ifinput_num=='y':
main()
else:
exit()

defmain():
choice=input("1.register.2.login.")
ifchoice=='1':
username=input("username:")
password=input("password:")
register(username,password)
elifchoice=='2':
username=input("username:")
password=input("password:")
login(username,password)
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: