错误:未定义全局名称**请帮助!**

问题描述 投票:0回答:1

**嗨,希望有人可以帮助我。我已在下面为我的老师在学校给我们的程序附加了Python代码。我继续遇到错误“未定义全局名称'passwordValid'”,并且找不到解决此问题的任何方法。在“ Password.append(passwordValid)”行中会发生此错误。任何帮助将不胜感激。非常感谢,里斯:)

global firstName
global lastName
global cateGory
global passwordValid
global passwordNew
firstName = ""
lastName = ""
cateGory = ""


def newMember():
#Recieve user input to assign values to variables
    firstName = str(input("Please enter your first name."))
    lastName = str(input("Please enter your last name."))

    #Validation to reject unspecifyed category input
    cateGoryValid = False
    while cateGoryValid == False:
        cateGory = str(input("PLease enter your category from the following:    (Junior,Adult,Senior)"))
        cateGory = cateGory.lower()
        cateGory = cateGory.capitalize()
        if cateGory == "Junior" or cateGory == "Adult" or cateGory == "Senior":
            cateGoryValid = True
        else:
            cateGory = str(input("PLease enter your category from the following: (Junior,Adult,Senior)"))

def passwordValidation():
    #initalising global variables
    #Creating Variables
    Valid = False
    firstCharacter =""
    firstValue = 0
    lastCharacter =""
    lastValue = 0
    passwordNew = ""
    passwordValid = []

    #Create conditional loop to validate the password
    while Valid == False:
        passwordNew = str(input("Please enter a new password"))
        #Assigns both the first and last values of the password as variables
        firstCharacter = passwordNew[0]
        lastCharacter = passwordNew[-1]
        #Assigns both values as ASCII characters
        firstValue = ord(firstCharacter)
        lastValue = ord(lastCharacter)
        #Ensures password is within boundaries using ASCII characters
        if firstValue >= 65 and firstValue and lastValue >= 35 and lastValue <=37:
            passwordValid = passwordNew
            Valid = True

    return passwordValid

def valuesAppend():
    return passwordValid
    global Forname
    global Surname
    global Category
    global Password

    Forename = [""]
    Surname = [""]
    Category  =[""]
    Password = [""]
    Position = 0
    Members = ["","","",""] * 11

    #Reading the text file
    file = open("members.txt","rt")

    #Create loop to assign all fields to a record
    for line in file:
    fields = line.split(",")
    Members[Position] = [fields[0],fields[1],fields[2],fields[3]]
    #Appending the fields into a global variable
    Forename.append(fields[0])
    Surname.append(fields[1])
    Category.append(fields[2])

    Position = Position + 1

#Assigning new variables into the record
Forename.append(firstName)
Surname.append(lastName)
Category.append(cateGory)
Password.append(passwordValid)

file.close

def categoryInfo():
    totalMembers = 0
    totalMembers = len(Forename)

    for counter in range(len(Forename)):
        print(Forename[counter], Surname[counter],Category[counter])
        print("There are:",Category.count("Junior"),"Junior members.")
        print("There are:",Category.count("Adult"),"Adult members.")
        print("There are:",Category.count("Senior"),"Senior members.")
        print("The current total is:",totalMembers,"members.")

def main():
    newMember()
    passwordValidation
    valuesAppend()
    categoryInfo()

main()
python compiler-errors global-variables undefined helper
1个回答
0
投票

您的代码的结构方式似乎有些错误。

首先,您不需要将变量定义为全局变量,只需要在函数的局部范围内使用'全局变量',这样它就知道它正在访问全局变量,例如]

password = "Password"
print(password) 
# prints "Password"

def update_pass():
    global password  # saying that the 'password' variable in this function refers to the global variable called 'password'
    password = "New password"

update_pass()
print(password)
# prints "New password"

第二,此代码块不属于任何函数,因此将尝试在main()函数之前运行。确保正确缩进]

#Assigning new variables into the record
Forename.append(firstName)
Surname.append(lastName)
Category.append(cateGory)
Password.append(passwordValid)

最后,这个功能很奇怪

def valuesAppend():
    return passwordValid
    global Forname
    # ...

[这里,您的valuesAppend()函数将尝试返回passwordValid,仅此而已,一旦返回发生,下面的其余代码将不会运行,因此您将无法从函数中获得任何其他用途

希望这会有所帮助

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