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?