1

I am in spyder trying to run a fairly simple python code (python 3.6.4), but when I run the code and use the input statements, it says the names are not defined, even though they should be.

#Module 4 project
#4/25/18
#Henry Degner
#This project is meant to determine whether or not certain people are qualified to go to an exclusive concert

#create function askAge, which will use an input statement and comparison operators to see if the user is old enough
def askAge():
    age = int(input("How old are you? "))
    if( age >= 18 ):
        ageReq = "true"
    elif( age < 18 ):
        ageReq = "false"
    else:
        print("Please print you're age with only numerical characters, in years")
        age = int(input("How old are you? "))
#create function askHearing, yes or no question to do you have sensitive hearing
def askHearing():
    hearing = str(input("Do you have sensitive hearing? "))
    if( str(hearing) == "yes","y" ):
        hearingReq = "do"
    elif( str(hearing) == "no","n"):
        hearingReq = "don't"
    else:
        print("Please type yes, or y, if you have sensitivehearing. If not, type no or n.")
        hearing = str(input("Do you have sensitive hearing? "))
#create function askTicket, yes or no question to do you have a ticket
def askTicket():
    ticket = str(input("Do you have a ticket for the concert? "))
    if( str(ticket) == "yes","y" ):
        ticketReq = "do"
    elif( str(ticket) == "no","n"):
        ticketReq = "don't"
    else:
        print("Please type yes if you have a ticket, or no if you don't.")
        ticketReq = str(input("Do you have a ticket for the concert"))
#create function askFun, yes or no question to are you willing to have an awesome time
def askFun():
    fun = str(input("Are you willing to have a great time?"))
    if( str(fun) == "yes","y"):
        funReq = "do"
    elif( str(fun) == "no","n" ):
        funReq = "don't"
    else:
        print("Please type 'yes' or 'no'")
        fun = str(input("Are you willing to have a great time?"))
#use def main():
def main():
#print situation, will ask some questions to see if you can attend concert
    print("Welcome to the Henry Degner Concert Venue! We have to ask you a few questions before we let you into the venue.")
#use askAge
    askAge()
#use askHearing
    askHearing()
#use askTicket
    askTicket()
#use askFun
    askFun()
#print user's answers
    print("You said you are " + age + " years old, you " + hearingReq + " have sensitive hearing, you " + ticketReq + " have a ticket, and you " + funReq + " want to have a great time!")
#use if-else statement, if all three conditions match requirements, print you can attend concert. If not, print you can't attend

#use main()
main()

the error outputs as

File "C:/Users/henry/Documents/Current Classes/Foundations of Programming/Module 4 project/Module 4 project script.py", line 60, in main
    print("You said you are " + age + " years old, you " + hearingReq + " have sensitive hearing, you " + ticketReq + " have a ticket, and you " + funReq + " want to have a great time!")

NameError: name 'age' is not defined

It also says that all of the other names in line 60 aren't defined. Thanks in advance!

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
user9705945
  • 11
  • 1
  • 3
  • 1
    You should read about [how scoping works in python](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules). You define `age` in the `askAge()` function, but once you leave that function the variable will not be available in `main()`. – pault Apr 26 '18 at 17:26
  • Someone is about to learn about return values :) – Mad Physicist Apr 26 '18 at 17:27

1 Answers1

2

You are getting NameError: name 'age' is not defined because variables from different functions are not shared until you declare them global or you return them from one method to another.

Read more about Python Scopes and Namespaces

Just return value from function to main method.

Returning value from method

def askAge():
    age = int(input("How old are you? "))
    if( age >= 18 ):
        ageReq = "true"
    elif( age < 18 ):
        ageReq = "false"
    else:
        print("Please print you're age with only numerical characters, in years")
        age = int(input("How old are you? "))

    return age

def main():
    ...
    age = askAge()
    .....

Same you need to do it for all methods,

askHearing()
askTicket()
askFun()
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
  • 2
    No, you don't. Because assigning to it in the function will make it local, and that's just bad advice. – Mad Physicist Apr 26 '18 at 17:28
  • @Aran-Fey. I was just sitting there trying to figure out how to say that :) – Mad Physicist Apr 26 '18 at 17:30
  • Please don't even mention global variables. They're a horrible solution for this problem. Talking about global variables here is a waste of time at best, and a newbie trap at worst. – Aran-Fey Apr 26 '18 at 17:40
  • @Aran-Fey thanks for suggestion. – Nishant Nawarkhede Apr 26 '18 at 17:41
  • Declaring the variables as global worked. It seems that variables defined in user-defined functions need to be declared as global before they can be used in the def main(): statement or any other functions. Thank you guys for your help, and I'll be sure to try to find an answer to questions like these before I post them. – user9705945 Apr 26 '18 at 17:52
  • 1
    @user9705945 as per Aran-Fey suggested don't use global variable. Please consider accepting answer. – Nishant Nawarkhede Apr 26 '18 at 17:55