Bird
0
0

How can you modify this code to stop the loop when the user enters 'exit'?

hard📝 Application Q9 of 15
Python - While Loop

How can you modify this code to stop the loop when the user enters 'exit'?

user_input = ''
while user_input != 'exit':
    user_input = input('Enter command: ')
AAdd break statement inside loop after input
BNo change needed; loop stops when 'exit' is entered
CChange condition to <code>while True</code> and use break
DInitialize user_input with 'exit' before loop
Step-by-Step Solution
Solution:
  1. Step 1: Understand loop condition

    The loop runs while user_input is not 'exit'.
  2. Step 2: Check input update

    Inside the loop, user_input is updated from user input each time.
  3. Final Answer:

    No change needed; loop stops when 'exit' is entered -> Option B
  4. Quick Check:

    Loop condition checks user input to stop [OK]
Quick Trick: Loop condition can directly check user input [OK]
Common Mistakes:
MISTAKES
  • Thinking break is required to stop loop
  • Initializing user_input incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes