Home > Back-end >  How do I clear a variable that I have referenced outside of my if statement?
How do I clear a variable that I have referenced outside of my if statement?

Time:08-01

while Guessp1 != Player1_secretword:
    Guessp1 = input("player 1, guess your secret word: ")
    nogp1  = 1
    if Guessp1 != Player1_secretword:
        hint1 = input("would you like a hint? (Y/N): ")
    if hint1 in ["yes", "y", "Y"]:
        hintcharecter = Player1_secretword[0]
        print("hint: "   hintcharecter)
        noghp1  = 1
    else:
        print("ok continue")

I want to clear the variable hint1 however when i try the "local variable "hint1" referenced outside the assesment" error, how do i fix this?.

CodePudding user response:

I think we need to restructure some things here.

  1. Add x = True statement before the loop. (Or whichever variable you prefer.)

  2. Change the "while" condition to be "while x == True".

  3. Move the "if" condition for hint1 to be a subset of the first if condition, instead of just following it. (In other words, the hint if condition should only appear if the user got the guess wrong in the first place.)

  4. Add an else statement to the Guessp1 if condition. "else: x = False"

This should break the while loop only if the correct word has been guessed. There may be some additional issues to contend with, depending on the rest of your code, but I think this should get you started.

Good luck!

  • Related