Python

Leah Miller
Leah Miller
10,372 Points

Wiki Links Extra Credit: I did it! Please give any feedback/suggestions on ways to make it better!

https://w.trhou.se/blzujaf12w

Thanks for checking it out! Kenneth Love

Here are some suggestions:

You are asking the user to input a date in format MM/DD and you also give them two other options to input. The other options are 'quit' and 'print'. You correctly check to see if the user entered 'quit' at the top of the while loop and exit appropriately. However, you are not then checking if the user entered 'print'. Instead, you are always printing outside the while loop. Run your code and test the following inputs: 'quit', mm/dd, 'print'. Think of 'print' as an elif option and this should work.

Spoiler Alert > I will also post my version of your code.

1 Answer

"""Enter a date and return it as a wiki link."""

import datetime

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

links = []
while True:
    answer = input("What dates would you like? Please use the MM/DD format. Enter 'quit' to quit. Enter 'print' to see list of links.  ")
    if answer.upper() == 'QUIT':
        break
    elif answer.upper() == 'PRINT':
        print("\nLinks:  ")        
        for link in links:
            print("   " + link)
    else:
        try:
            date = datetime.datetime.strptime(answer, answer_format)
            output = link.format(date.strftime(link_format))
            links.append(output)
        except ValueError:
            print("That's not a valid date. Please try again.")