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 Dates and Times in Python (2014) Dates and Times Dating Methods

My Solution in the wikipedia-link challenge

Hello, this was my simple implementation of creating a script that lets the user enter a date and a month in a specifc format, then convert the str object to a datetime object and then extrapolate the desired data and insert it in the wikipedia link through the strftime method.

from datetime import datetime


loop= True

while loop:
    date = input("Please enter a month and day in the format (DD/MM)")
    try:
        date = datetime.strptime(date, "%d/%m")
    except ValueError as e:
        print(e)
    else:
        print("Here is a wikipedia link to that date: "
              "https://en.wikipedia.org/wiki/{}".format(
               date.strftime("%B_%d")))
        loop = False

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Nice! With a slight change, the loop variable may be eliminated by using:

while True:
    try:
        # ...
    else:
        # ....
        break