Python Dates and Times in Python 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
Chris Freeman
Chris Freeman
Treehouse Moderator 65,221 Points

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

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