但是我试图使用该语句,(如果new_username == username)。我想使用他们已创建的用户名和他们刚刚输入的用户名。然而,这2个输入处于不同的程序中。因此,它不会让我使用来自不同程序的另一个输入。如果我可以对代码进行任何更改或编辑以克服此问题,我将不胜感激!
您的测试不正确,您应该使用用户名和密码作为全局变量。
试试这个 :
def register():
username = input("Please enter your username ")
password = input("Please enter your password ")
file = open("login.txt","a")
file.write(username+" "+password+"\n")
file.close()
def login():
username = input("Please enter your username ")
password = input("Please enter your password ")
for line in open("login.txt","r").readlines():
login_info = line.split()
if username == login_info[0] and password == login_info[1]:
print("Correct credentials!")
return True
print("Incorrect credentials.")
return False
在newUser()函数中,您必须分配已使用的变量,例如'username'为全球。这将允许您使用和访问当前过程之外的变量。
def newUser():
global username
username = input("Create login name: ")
对要在当前过程之外访问的每个变量执行此操作。
理想情况下,如果您要在程序中执行此操作,可能需要查看使用类。