Bird
Raised Fist0

Find the bug in this code snippet:

medium📝 Debug Q7 of Q15
Python - Standard Library Usage
Find the bug in this code snippet:
from datetime import datetime, timedelta
now = datetime.now()
new_time = now + '5 days'
print(new_time)
AMissing parentheses in datetime.now
BCannot add string '5 days' to datetime object
Ctimedelta should be imported from time module
Dprint() syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the addition operation

    Adding a string '5 days' to a datetime object is invalid in Python.
  2. Step 2: Correct way to add days

    Use timedelta(days=5) to add 5 days to a datetime object.
  3. Final Answer:

    Cannot add string '5 days' to datetime object -> Option B
  4. Quick Check:

    Add timedelta, not strings, to datetime [OK]
Quick Trick: Use timedelta(days=5), not strings, to add days [OK]
Common Mistakes:
MISTAKES
  • Adding strings to datetime
  • Forgetting parentheses in now()
  • Wrong import source for timedelta

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes