我正在学习构建一个python脚本来检查文本文件中的用户名和密码,以及是否正确记录用户。将有两个用户和文件将有多个用户。 我有工作的脚本,但我有一些密码检查问题。我想让函数在密码输入错误的情况下进行循环。 如果我在函数中这样做而陷入同一个循环,我只能通过从函数中混合并再次调用它来让它工作
下面是有效的脚本。如果密码不正确,则返回0,0,因此调用函数
user1Status = 0
user2Status = 0
def logging():
filex = open('users.txt', 'r')
x = filex.read() # This will store the contents of the file as a string
filex.close()
login_info = x.split()
numberUsers = (len(login_info))
username = input("enter username")
count = 0
for x in range(0, numberUsers):
if username != login_info[x]:
count = count + 1
if numberUsers == count:
print('Username not recognised')
return 0, 0
password = input("enter password")
for x in range(0, numberUsers):
if username == login_info[x] and password == login_info[x + 1]:
status = 1
return username, status
else:
print('Your password is incorrect')
#logging()
return 0, 0
while 1:
if user1Status == 0:
print ('User1 please logging')
username1, user1Status = logging()
if user1Status == 1 and user2Status == 0:
print('User2 please logging')
username2, user2Status = logging()
if (user1Status == 1) and (user2Status == 1):
print("Welcome back %s and %s" % (username1, username2))#
如果我注释掉logging函数末尾的return 0,0和uncomment logging(),它将继续在这个函数中循环,如果输入了不正确的密码,则返回正确的密码。这是一个不能这样下去的东西
我这么做的原因是。如果密码输入不正确,我不想再次询问用户是否有帐户
我用下面的脚本设法使它工作。我添加了一个if语句来检查密码。这是完整的代码
相关问题 更多 >
编程相关推荐