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 trial

Python

wiki.py: Need an explanation of the code in the video

Good Afternoon!

I have difficulties to understand this code example from this video: https://teamtreehouse.com/library/dates-and-times-in-python/dates-and-times/wikipedia-links

Here is the teacher's code:

import datetime

answer_format = "%m/%d"
link_format = "%b_%d"
link = "https://en.wikipedia.org/wiki/{}"



while True:
    answer = input("What date would you like? Please use the MM/DD format. Enter 'quit' to quit.")
    if answer.upper() == "QUIT":
        break


    try:
        date = datetime.datetime.strptime(answer, answer_format)
        output = link.format(date.strftime(link_format))

        print(output)
    except ValueError:
        print("That's not a valid date. Please try again.")

I have several questions:

1.) Can somebody explain the whole code in the try block? And break the steps down for me? 2.) Can somebody explain me when to use what? (Like I know all these different datetime methods but I mix them all the time up.)

Thanks in Advance, Alexandra

1 Answer

Steven Parker
Steven Parker
243,656 Points

I'll try to break down those lines:

        date = datetime.datetime.strptime(answer, answer_format)
#                                         answer                 <- the string typed in
#                                                 answer_format  <- the format it should be in
#              datetime.datetime.strptime(answer, answer_format) <- convert it to a date
#                                                                   (any problems converting may
#                                                                    cause a ValueError exception.)
#       date =                                                   <- save it in this variable
        output = link.format(date.strftime(link_format))
#                            date                                <- the date from above
#                                          link_format           <- the format we want it in
#                            date.strftime(link_format)          <- convert it to a string
#                link                                            <- a string with placeholder
#                link.format(date.strftime(link_format))         <- "fill in the blank"
#       output =                                                 <- save it in this variable

I can never remember every function, either! Make yourself a "cheat sheet", or find a good documentation summary online and add it to your bookmarks.

Thank you really much! Now I understand it better!