Bird
0
0

How can you use a for-else loop to find if a list contains any negative numbers and print Found negative or No negatives accordingly?

hard📝 Application Q9 of 15
Python - For Loop
How can you use a for-else loop to find if a list contains any negative numbers and print Found negative or No negatives accordingly?
Afor num in lst: if num < 0: print('Found negative') else: print('No negatives')
Bfor num in lst: if num < 0: print('No negatives') break else: print('Found negative')
Cfor num in lst: if num >= 0: print('Found negative') break else: print('No negatives')
Dfor num in lst: if num < 0: print('Found negative') break else: print('No negatives')
Step-by-Step Solution
Solution:
  1. Step 1: Detect negative numbers with break

    Loop through list, if a negative is found, print 'Found negative' and break.
  2. Step 2: Use else to print if no break

    If loop finishes without break, print 'No negatives' in else block. for num in lst: if num < 0: print('Found negative') break else: print('No negatives') matches this logic.
  3. Final Answer:

    for num in lst: if num < 0: print('Found negative') break else: print('No negatives') -> Option D
  4. Quick Check:

    Break on negative, else means none found [OK]
Quick Trick: Break on condition met, else means condition never met [OK]
Common Mistakes:
MISTAKES
  • Swapping messages for found and not found
  • Missing break causing multiple prints
  • Placing else inside loop incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes