Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialZinia Khondoker
Python Development Techdegree Graduate 10,451 PointsHarder Time Machine
Is there a way to solve this using the f string? I've excluded the year for now and just curious to see if this works
import datetime
starter = datetime.datetime(2015, 10, 21, 16, 29)
def time_machine(int, "minutes"or "hours" or "days"):
timedelta = datetime.timedelta(f'{"minutes"or "hours" or "days"}={int}')
duration = starter + timedelta
return duration
1 Answer
Steven Parker
231,110 PointsFirst, the declaration needs the second parameter to be a name instead an expression using string literals.
Then, the timedelta needs a keyword and value pair (a "kwarg") instead of a string argument. If you want to get fancy, you can build a dictionary object and then unpack it to create the argument.
When you do make an f-string (in other exercises), you can create a substitution token by surrounding its name with braces. You can also make strings using format() or concatenation.
Gerald Bishop
Python Development Techdegree Graduate 16,897 PointsGerald Bishop
Python Development Techdegree Graduate 16,897 PointsI have also tried this f string approach but still cannot figure out how to make it work. My code gives a TypeError. Please help
Steven Parker
231,110 PointsSteven Parker
231,110 PointsInstead of a string, the timedelta should be given a keyword/value pair or an unpacked dictionary.
Also, don't forget you need to handle "years" as a special case.
And, you won't need to print anything for this challenge.