0

I am making a arithmetic quiz in python. At the start of the quiz it asks the user what class they would like to input results for, this is in the def classname(). Later on when the quiz is finished, the program will write the scores to a text file if the user does not want to repeat the quiz like this:

def classname():
    class_name = input("Which class do you wish to input results for?")
    #  the rest of my code for my introduction 
    #
    #
    #
    #
def askquestion():
    # all of my code to ask the user the arithmetic question
    #
    #
    #
    # code to ask the user if they want to repeat the quiz
    #if they dont want to repeat the quiz then this code is run
    else:
        filename = class_name + ".txt"
        # code that will write the scores to a text file

When i run this code i get this error:

   filename = class_name + ".txt"
 NameError: name 'class_name' is not defined

Do I have to declare the variable "classname" again in askquestion() or is there a way python can recognise I have already declared the variable?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
John abey
  • 35
  • 7

3 Answers3

1

Unless you define your variable as global, you will have to redefine it, or pass the value in your code to subsequent functions as an argument.

You can pass in an argument to askquestion, as it currently stands, the variable class_name is out of scope for the function .

So, your function definition changes to

def askquestion(class_name):
    ...
    ...

And now, when you call the askquestion function, you will have to pass the class_name to it.


A working example will look something like below:

def classname():
    class_name = input("Which class do you wish to input results for?")
    ...
    ...
    return class_name

def askquestion(class_name):
    ...
    else:
        filename = class_name + ".txt"
        # code that will write the scores to a text file

if __name__ == `__main__`:
    class_name = classname()
    askquestion(class_name)
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • doesn't work when im calling the ask question method gives me the error: `totalScore += askquestion(class_name)` `NameError: name 'class_name' is not defined` – John abey Apr 11 '15 at 15:15
  • 1
    @Johnabey That's a separate error from the one you already had, you will need to post the complete code for this one to be resolved. – Anshul Goyal Apr 11 '15 at 15:17
  • Everything works but can you just tell me what `if __name__ == "__main__":` and all of the code inside it does – John abey Apr 12 '15 at 23:16
  • I also wanted to ask, out of all the answers is you're code the most efficient, as in, does it use the least lines of code. – John abey Apr 12 '15 at 23:30
  • @Johnabey Check http://stackoverflow.com/q/419163/1860929. Basically everything within that block is executed only if you directly call the module. Regarding efficiency, yes I think this is most efficient among current answers. If that helped, don't forget to [accept and upvote](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) :) – Anshul Goyal Apr 25 '15 at 04:50
0

Your code declares the variable class_name inside the class_name() function, so it's not accessible outside. If you declare the variable class_name outside the class_name() function, it will be accessible to the askquestion() function.

livibetter
  • 19,832
  • 3
  • 42
  • 42
0

Variables declared inside a function are local to that function and either need to be passed or returned to other methods or moved outside of the function to make it global, which needs to be explicitly declared when using it inside a function:

So you could return the class_name from classname() and use classname() in askquestion():

def classname():
    class_name = input("Which class do you wish to input results for?")
    return class_name

def askquestion():
    ...
    else:
        filename = classname() + ".txt"
        # code that will write the scores to a text file   
AChampion
  • 29,683
  • 4
  • 59
  • 75