Bird
0
0

You want to stop searching a list as soon as you find the number 7. Which code correctly uses break to do this?

hard📝 Application Q8 of 15
Python - Loop Control
You want to stop searching a list as soon as you find the number 7. Which code correctly uses break to do this?
numbers = [3, 5, 7, 9, 11]
for num in numbers:
    # Your code here
Aprint(num) if num == 7: break
Bif num == 7: break print(num)
Cif num != 7: break print(num)
Dbreak if num == 7 print(num)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to print numbers until we find 7, then stop immediately.
  2. Step 2: Check each option's logic

    print(num) if num == 7: break prints the number first, then breaks if it is 7, stopping after printing 7.
  3. Final Answer:

    print(num)\nif num == 7:\n break -> Option A
  4. Quick Check:

    Print then break on 7 = print(num) if num == 7: break [OK]
Quick Trick: Print before break to include the break value [OK]
Common Mistakes:
MISTAKES
  • Breaking before printing the found number
  • Using invalid syntax for break
  • Breaking on wrong condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes