CS50P PS3 outdated.py exercise - deciphering check50 frowny smiley and comment

657 Views Asked by At

So I did a sort of lazy solution of the PS3 outdated exercise by making use of a library related to working with dates and times (trying not to spoiler anything here).

Unless I am missing something, my program works as it should. However, check50 gives me a :( on one query:

:( input of " 9/8/1636 " outputs 1636-09-08 Did not find "1636-09-08" in "Date: "

So my code returns "1636-09-08" if the user inputs "9/8/1636" which seems right to me. Can anyone shed some light on how to interpret the second line of check50's output? Thanks.

2

There are 2 best solutions below

0
badCoder On

I can't remember the question exactly but here is my solution.

`months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]

convert valid user date to output

try: month, day, year = input("Date: ").split("/")

if int(month) < 10:
    month = f"0{month}"

if int(day) < 10:
    day = f"0{day}"

# split on /
print(f"{year}-{month}-{day}")

except: str_date = input("Date: ") print(str_date)`

1
diego bendezu On

Here's my solution

months = {

"January": "1",
"February": "2",
"March": "3",
"April": "4",
"May": "5",
"June": "6",
"July": "7",
"August": "8",
"September": "9",
"October": "10",
"November": "11",
"December": "12"

}

def main():

while True:
    try:
        date = input("Date: ")
        if date[0].isdigit():
            x, y, z = date.split("/")
            x = int(x)
            y = int(y)
            z = int(z)
            if x <= 12 and y <= 31:
                print(f"{z}-{x:02}-{y:02}")
                break
        else:
            x, y, z = date.split(" ")
            if x in months:
                x = months[x]
                y = y.replace(",", "")
                print(f"{z}-{x}-{y}")
                break
    except:
        pass

main()