Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Dates and Times in Python Let's Build a Timed Quiz App Harder Time Machine

Zinia Khondoker
Zinia Khondoker
3,508 Points

Harder 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
Steven Parker
218,596 Points

Before getting to the f-string, the declaration needs the second parameter to have a name instead an expression using string literals.

Then, in your f-string, you can include that 2nd argument by surrounding its name with braces.

If you're unsure about creating f-strings, try making the code work using format() or concatenation first and then you can convert either into an f-string later.