您如何使用一个过程与另一个过程中的输入(在if语句内)

时间:2019-03-07 20:27:12

标签: python input login server procedure

我的项目正在使用txt文件制作登录服务器,以存储用户信息并进行追溯以完成登录。

但是我尝试使用语句(如果new_username == username)。我想使用他们已经创建的用户名和他们刚刚输入的用户名。但是,这两个输入的步骤不同。因此,它不会让我使用来自不同过程的另一个输入。 如果可以对代码进行任何更改或编辑以解决此问题,我将不胜感激!

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

original image of my code so far

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

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

理想情况下,如果您打算在程序中执行很多操作,则可能要看看使用类。