NameError: Variable is not defined


I am getting the error:
NameError: name ‘age’ is not defined but when i look at my code i have it set equaled to age = 0

#Declare main module
def main():
    age = 0
    weight = 0
    month = ()


def intro():
    print("Welcome to Guess the secrects! ")
    print("""I want you to guess the age, wieght, and birth month to
uncover my secrects!""" )

    #ask user for input
def get_age():
    age = input("Enter age. ")
    return age

def get_weight():
    weight = input("Enter weight. ")
    return weight

def get_month():
    month = input("Enter a birth month. ")
    return month

    # We will now determine if the user guess the correct answers
    #using if statements.
def correct_answers():
    if age <= 25:
        print("Congratulations, the age is 25 or less. ")
    if weight >= 128:
        print("Congratulations, the weight is 128 or more. ")
    if month == "April":
        print("Congratulations, the birth month is April. ")



variable scope, age is a local variable, it only exist within the main() function


What do I do to fix this error.


modules (each file) and functions have their own variable scopes, this is so that different pieces of code don’t interfere with each other (it would be a mess if everything was global)

A module typically contains functions, classes, constants, and functions typically have variables related to what they are doing

When a function has result of some kind, they’ll typically return that value. The caller can then use that result as it sees fit, perhaps they’ll send it to other functions – this would fit your code rather well, that the input-getting functions return their results and that you then pass those values to your result-checking function


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.