1

I'm new in python. I'm trying to code a basic hangman game, the thing is, when I put on purpuse a wrong answer, my for loop keep making the iteration as if nothing happen, I'd like that each time I mistake, the loop "freeze" o keep in the same letter until I put the correct answer.

This is my code:

word = input("Introduce a word ")

n = len(word)
print("The word has "+ str(n) + " letters")
print("Hint: The first letter is " + word[0])
print("")

for i in word:
    print(i)
    if i == input("Guess the letter: "):
        print("Correct")
    else:
        print("Wrong !")
martineau
  • 119,623
  • 25
  • 170
  • 301
Thrillidas
  • 79
  • 7

1 Answers1

2

Loop forever and break from the loop if the answer is correct.

for character in word:
    print(character)
    while True:
        if character == input("Guess the letter: "):
            print("Correct")
            break
        else:
            print("Wrong !")

The while part of the code in real world language: "I'm going to ask you forever until you give me the correct answer."

Matthias
  • 12,873
  • 6
  • 42
  • 48
  • Thanks you, and thanks to everybody that took a little time to answer this little and silly question ! I have a lot to learn, but I'm motivated thanks for current comunity :) – Thrillidas Sep 25 '20 at 20:32
  • Oh no, that was not a silly question if you're new. Over the time you'll see that a `while True` loop is used quite often in Python code. – Matthias Sep 26 '20 at 04:29