Essentially I am making a quiz, and python returns an error saying the variable shown below is undefined when it has been defined.
def guesscheck(guess, answer):
correct = "null"
print(lives * '\u2665')
if guess.upper() == answer:
print("Marvelous! That is correct!")
correct = True
return correct
if guess.upper() != answer:
print("That is incorrect.")
correct = False
return correct
lives = 3
guess = input("ok: ")
guesscheck(guess, "OK")
if correct == False:
lives = lives - 1
print(lives * '\u2665')
As you can see, I call the function which should define the variable 'correct' as True or False and return it to the rest of the program, but for some reason it is showing as undefined. Please help me!
correctis in local scope to the function, try addingglobal correctat the top of function definitionguesscheckfunction. See docs.python.org/3/tutorial/…return <variable>statement does return something, but neither the name of a variable, nor a value, but a reference. And in order to use the reference you have to assign it to a variable of your own as the many answers suggest.