๐Ÿ PyQuest๐Ÿ” Loops Loops Loops ยท ๐Ÿช„ The Magic Word
โญ 0 XP๐Ÿ Python is waking upโ€ฆ

The Magic Word ๐Ÿช„

while loops are perfect for asking a question until you get the answer you want. A for loop can't do this โ€” you have no idea how many guesses someone will need!

answer = ""

while answer != "banana":
    answer = input("Say the magic word: ")

print("๐ŸŽ‰ You said it!")

Here's the trick: you have to give answer a starting value before the loop, or Python doesn't know what to compare on the very first check. An empty string "" is perfect โ€” it's definitely not the magic word, so the loop is guaranteed to run at least once.

Each turn, input() replaces answer with whatever they typed. The moment it matches, the comparison turns False and the loop lets go.

Your mission

  1. Make a variable magic_word with your secret word, and a variable guess that starts out empty: guess = ""
  2. Write a while loop that keeps calling input() until guess matches magic_word
  3. Inside the loop, use an if to tease them when the guess is wrong
  4. After the loop, print a big celebration

Run it and get it wrong a few times on purpose. That's the whole point! ๐Ÿ˜„

Your code

Press โ–ถ Run to see what your code does!