Bird
0
0

Find the mistake in this Raspberry Pi time-lapse code snippet:

medium📝 Debug Q6 of 15
Raspberry Pi - Camera Module
Find the mistake in this Raspberry Pi time-lapse code snippet:
from picamera import PiCamera
from time import sleep
camera = PiCamera()
for i in range(4):
    camera.capture('photo_i.jpg')
    sleep(2)
APiCamera() must be initialized inside the loop
Bsleep(2) should be called before camera.capture()
CThe filename string does not use f-string formatting to include the loop variable
DThe range should be from 1 to 5 instead of 0 to 3
Step-by-Step Solution
Solution:
  1. Step 1: Analyze filename usage

    The filename 'photo_i.jpg' is a literal string and does not include the value of i
  2. Step 2: Correct string formatting

    Use an f-string like f'photo_{i}.jpg' to insert the loop variable
  3. Step 3: Confirm other code parts

    sleep(2) after capture is valid; camera initialization outside loop is efficient
  4. Final Answer:

    The filename string does not use f-string formatting to include the loop variable -> Option C
  5. Quick Check:

    Use f-strings to embed variables in filenames [OK]
Quick Trick: Use f'filename_{i}.jpg' to include loop index [OK]
Common Mistakes:
MISTAKES
  • Using quotes without f-string for dynamic filenames
  • Reinitializing camera inside the loop unnecessarily
  • Changing sleep order without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes