Python:我的全局变量未定义

我试图创建一个registerFunction将用户名和密码保存到列表中。但是,当我在while循环中调用username_list(位于readFunction中)时,收到错误消息“未定义username_list”

def readFunction(): #--------------------------------------------------readFunction
    username_read = open('username_db.csv', 'r')
    global username_list
    username_list = [n.replace("\n","") for n in username_read.readlines()]#<<<-------------------------Where the variable is created is produced
    username_read.close()

    password_read = open('password_db.csv', 'r')
    global password_list
    password_list = [n.replace("\n", "") for n in password_read.readlines()]
    password_read.close()


def registerFunction(): #----------------------------------------------registerFunction
    print("\nRegister - Please enter your details below\n")
    username = input("Enter a Username: ")
    password = input("Enter a Password: ")
    passwordConfirm = input("Confirm your password: ")

    while username in username_list: #<<<-------------------------Where the error is produced
        print("Sorry, this username is already in use")
        username = input("Enter a Username: ")
    else:
        username_list.append(username)
        password_list.append(password)
        print("Your account has been successfully created")
评论