Python 密码和注册码

Python 密码和注册码,python,Python,我正在为注册和密码系统编写代码。然而,在第一个注册的人被写入文件后,下一个注册的人也将被写入文件,但当涉及到登录时,他们不能。 请忽略打印(“”)间隙和时间暂停 import time def register(): username = input("Please enter a username: ") print('') password = input("Please enter your desired password: ") print('')

我正在为注册和密码系统编写代码。然而,在第一个注册的人被写入文件后,下一个注册的人也将被写入文件,但当涉及到登录时,他们不能。 请忽略打印(“”)间隙和时间暂停

import time
def register():
    username = input("Please enter a username: ")
    print('')
    password = input("Please enter your desired password: ")
    print('')
    file = open("accountfile3.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    if login():
        print("You are now logged in...")
    else:
        print("You aren't logged in!")
def login():
    x = False
    while x == False:
        username = input("Please enter your username: ")
        print('')
        password = input("Please enter your password: ")
        print('')
        for line in open("accountfile3.txt","r").readlines(): # Read the lines
            login_info = line.split() # Split on the space, and store the results in a list of two strings
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            return True

        break
    print("Incorrect credentials.")
    print('')
print('Hello')
print('')
time.sleep(1)
print('This is python_workspace')
print('')
time.sleep(1)
print('These are the choices available: ')
print('')
print('|' + '-'*24 + '|')
print('|    1. Register         |')
print('|                        |')
print('|    2. Login            |')
print('|                        |')
print('|    3. Exit             |')
print('|' + '-'*24 + '|')
y = True
while y == True:
    choice1 = input('Please enter the number: ')
    print('')
    if choice1 == '1':
        register()
        break
    elif choice1 == '2':
        login()
        break
    elif choice1 == '3':
        break
    else:
        print('Incorrect input, please enter either: 1, 2, 3')
        print('')

这是由于在文本文件中循环时缩进造成的。它只读了一行,这就是它失败的原因。我已经在下面的代码中修复了它。然而,像这样存储用户名和密码可能不是最好的主意,我建议为此使用数据库。无论何时处理密码,都应该使用强散列函数对其进行散列。此外,对用户名的验证也非常重要,例如用户名中可能有空格,因此无法正确读取密码

import time
def register():
    username = input("Please enter a username: ")
    print('')
    password = input("Please enter your desired password: ")
    print('')
    file = open("accountfile3.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    if login():
        print("You are now logged in...")
    else:
        print("You aren't logged in!")
def login():
    x = False
    while x == False:
        username = input("Please enter your username: ")
        print('')
        password = input("Please enter your password: ")
        print('')
        for line in open("accountfile3.txt","r").readlines(): # Read the lines
            login_info = line.split() # Split on the space, and store the results in a list of two strings
            if username == login_info[0] and password == login_info[1]:
                print("Correct credentials!")
                return True

        break
    print("Incorrect credentials.")
    print('')
print('Hello')
print('')
time.sleep(1)
print('This is python_workspace')
print('')
time.sleep(1)
print('These are the choices available: ')
print('')
print('|' + '-'*24 + '|')
print('|    1. Register         |')
print('|                        |')
print('|    2. Login            |')
print('|                        |')
print('|    3. Exit             |')
print('|' + '-'*24 + '|')
y = True
while y == True:
    choice1 = input('Please enter the number: ')
    print('')
    if choice1 == '1':
        register()
        break
    elif choice1 == '2':
        login()
        break
    elif choice1 == '3':
        break
    else:
        print('Incorrect input, please enter either: 1, 2, 3')
        print('')

建议您使用第三方身份验证服务。以Auth0提供的产品为例


您的目的是将明文密码写入txt文件?缩进是否正确?为什么您的
用于?如果username==login\u info[0]和password==login\u info[1]:
块缩进不正确。还有,dfundako所说的。我希望这只是一个家庭作业或类似作业的玩具程序。请不要为一个真正的登录系统这样做。我有一个很快笑出这个。。。明文密码存储…:这真的有必要吗。OP显然是一个全新的学习者。提供那个链接作为附加信息是很好的,但作为一个独立的回答,我发现它完全没有帮助。@EricChen1248,在大多数情况下我同意你的看法。但是说到身份验证,我坚信没有人,尤其是初学者,应该建立自己的身份验证。最初的帖子表明该代码将投入生产。提出的代码是危险的。OP最好花时间学习如何使用可靠的第三方服务。thx这非常有用,我还将接受您关于存储用户名和密码的建议