Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name "userInput" cannot be defined

i'm practicing some Python and i'm having some trouble trying to compare userInput would selected word in a hangman game.

# User guess input (single char)
# Exception handling to limit input to letters between a-z
while True:
    userInput = str(input('Please guess a letter:'))
    if len(userInput) == 1:
        if not re.match("^[a-z]*$", userInput):
            print("Error! Only letters a-z allowed!")
            continue
        break
    else:
        print("Error! Please input a single letter!")
        continue
# Comparing array with input
for i in range(len(selWord)):
    if(userInput == selWord[i]):

The problem lies in the in the last line:

        if(userInput == selWord[i]):

It states that "The name 'userInput' is not defined." however when I try to print outside the initial while loop, it works fine.

like image 549
redouane Avatar asked May 10 '26 07:05

redouane


1 Answers

set userInput = None above while loop

This was the solution, thank you everyone!

like image 159
redouane Avatar answered May 12 '26 20:05

redouane