-1

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!

7
  • 1
    hard to tell since you haven't formatted the code, but seems that the issue is that correct is in local scope to the function, try adding global correct at the top of function definition Commented Aug 7, 2021 at 15:21
  • 3
    It's only defined within the body of the guesscheck function. See docs.python.org/3/tutorial/… Commented Aug 7, 2021 at 15:21
  • @Mattiiss I will try that. Commented Aug 7, 2021 at 15:22
  • 1
    Even if so, where are you using a loop in your code? Commented Aug 7, 2021 at 15:30
  • @JustANoobProgrammer, Yes the 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. Commented Aug 7, 2021 at 15:34

3 Answers 3

1

You should return the correct variable from your function and then access it when calling guesscheck:

def guesscheck(guess, answer):
    # This variable declaration is not necessary
    # correct = "null"
    #
    # Instead, you can assign it to the answer check
    correct = guess.upper() == answer

    print(lives * '\u2665')

    if correct:
        print("Marvelous! That is correct!")

    # You can use an else-clause here
    else:
        print("That is incorrect.")

    return correct

Then access it like this:

lives = 3
guess = input("ok: ")
correct = guesscheck(guess, "OK")
# No need for `correct == False`
if not correct:
    lives = lives - 1
    print(lives * '\u2665')
Sign up to request clarification or add additional context in comments.

2 Comments

Is "if not correct:" essentially a rewritten version of "correct == False:"?
Actually, not correct also checks for additional things, such as empty strings and empty lists, while correct == False strictly checks for the boolean value False, but since guesscheck only returns a boolean, in your case they are equivalent.
1

Try the below (note how correct is used in the main loop)

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
while lives:
    guess = input("ok: ")
    correct = guesscheck(guess, "OK")
    if not correct:
        lives = lives - 1
        print(lives * '\u2665')

2 Comments

Thank you, however, the code keeps running forever, and I need it to stop at some point so that I can put more than one question into the quiz code.
@JustANoobProgrammer I can see no reason for making correct global - isnt it?
1

You do not define correct outside of the function, it is only available on the local scope.

Assigning on the outside scope should fix your problem.

correct = guesscheck(guess, "OK")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.