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.
set userInput = None above while loop
This was the solution, thank you everyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With