Reputation: 39
If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?
This is what I have tried:
>>> import datetime
>>> t=datetime.time(6,52)
>>> print (t)
06:52:00
>>> b=t+datetime.timedelta (8 hours,15 minutes)
File "<stdin>", line 1
b=t+datetime.timedelta (8 hours,15 minutes)
^
SyntaxError: invalid syntax
````````````````````````````````````````````````````
Upvotes: 0
Views: 457
Reputation: 4079
So let's walk it back a few steps. When you tried b=t+datetime.timedelta (8 hours,15 minutes)
, what you were trying to do was increment your time object by 8 minutes and 15 seconds, using the timedelta
function. In Python, functions take arguments, and timedelta
, like any other function, has specific kinds of values you can pass for arguments. You can find them here, because since datetime
is a library, it has nice documentation for everything. It looks like you were using IDLE which also gives you a peek at type hints:
So now we know that timedelta
takes any one of days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0
.
In our case, we want to add 8 minutes and 15 seconds to our original datetime object.
That would mean setting minutes=8
and seconds=15
.
So when we call the timedelta
function, to avoid that syntax error, we want to invoke the function like such:
b=t+datetime.timedelta(minutes=8, seconds=15)
And there you have it! Just remember what is and is not valid syntax in Python. Python doesn't know a thing about what you mean when you give it plain English.
Upvotes: 3