Bird
0
0

Which code snippet correctly uses a while True loop to repeatedly ask the user to input a word and stops only when the user types 'exit'?

hard📝 Application Q8 of 15
Python - While Loop

Which code snippet correctly uses a while True loop to repeatedly ask the user to input a word and stops only when the user types 'exit'?

Awhile True: word = input('Enter a word: ') if word == 'stop': break
Bwhile True: word = input('Enter a word: ') if word != 'exit': break
Cwhile True: word = input('Enter a word: ') if word == 'exit': break
Dwhile True: word = input('Enter a word: ') if word == 'Exit': break
Step-by-Step Solution
Solution:
  1. Step 1: Understand the stopping condition

    The loop should stop only when the user inputs exactly 'exit'.
  2. Step 2: Analyze each option

    while True: word = input('Enter a word: ') if word == 'exit': break breaks when word == 'exit' (correct). while True: word = input('Enter a word: ') if word != 'exit': break breaks when word != 'exit' (wrong). while True: word = input('Enter a word: ') if word == 'stop': break breaks on 'stop' (wrong word). while True: word = input('Enter a word: ') if word == 'Exit': break breaks on 'Exit' (case sensitive, wrong).
  3. Final Answer:

    while True: word = input('Enter a word: ') if word == 'exit': break correctly implements the required behavior.
  4. Quick Check:

    Break only if input equals 'exit' [OK]
Quick Trick: Break loop only when input matches exact stop word [OK]
Common Mistakes:
MISTAKES
  • Using wrong stop word
  • Incorrect condition logic (e.g., != instead of ==)
  • Ignoring case sensitivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes