如何使用不同过程的一个过程使用输入(在if语句中)

问题描述 投票:-1回答:2

My project is making a login server using txt files to store user info, and trace back, to complete logins.

但是我试图使用该语句,(如果new_username == username)。我想使用他们已创建的用户名和他们刚刚输入的用户名。然而,这2个输入处于不同的程序中。因此,它不会让我使用来自不同程序的另一个输入。如果我可以对代码进行任何更改或编辑以克服此问题,我将不胜感激!

my code, and with drawings to explain what im talking about

original image of my code so far

python input login server procedure
2个回答
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

0
投票

在newUser()函数中,您必须分配已使用的变量,例如'username'为全球。这将允许您使用和访问当前过程之外的变量。

def newUser():
    global username 
    username = input("Create login name: ")

对要在当前过程之外访问的每个变量执行此操作。

理想情况下,如果您要在程序中执行此操作,可能需要查看使用类。

© www.soinside.com 2019 - 2024. All rights reserved.