0

I can't seem to get passed PSET3 outdated.py. I get two errors when running the check50 command:

:( input of " 9/8/1636 " outputs 1636-09-08 expected "1636-09-08", not "1636 -09-08\n"

I don't know where the extra space is coming from nor the line break at the end.

:( input of September 8 1636 results in reprompt expected program to reject input, but it did not

I am not sure what the problem here is, other than a missing comma.

Here is my code:

def main():
months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

while True:
    date = input("Date: ")

    try:
        m, d, y = date.split("/")
        if (int(m) > 0 and int(m) < 13) and (int(d) > 0 and int(d) < 32):
            break

    except:
        try:
            alt_m, alt_d, y = date.split(" ")
            for month in range(len(months)):
                if alt_m == months[month]:
                    m = month + 1

            d = alt_d.replace(",", "")

            if (int(m) > 0 and int(m) < 13) and (int(d) > 0 and int(d) < 32):
                break
        except:
            print()
            pass

print(f"{y}-{int(m):02}-{int(d):02}")

main()

I looked though the forum and I also found this Youtube video, which used a similar methodology, interestingly it works in the video tutorial, but reading the comments shows that others have the same problem.

2
  • I can't get my head around what's the use of "02" here, in the last line - print(f"{y}-{int(m):02}-{int(d):02}"). Commented Jul 1, 2023 at 2:46
  • @TheQuietKid because we want leading zero's
    – Jay
    Commented Aug 19, 2023 at 6:02

4 Answers 4

5

The input " 9/8/1636 " contains the (trailing) space and the program does not account for that, therefore it is in the output as entered.

"other than a missing comma." is a good guess because the spec says:

prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636 .......
If the user’s input is not a valid date in either format, prompt the user again.

2
  • Thank you kindly. I didn't notice the space. What would be the best way to implement it? Commented Jun 27, 2022 at 8:26
  • Thanks for the answer. I am surprised that my code works, although I do not explicitly take care of possible whitespace.. I did not want to spam this thread so I created a separate question, and greatly appreciate any feedback .. My question is here: cs50.stackexchange.com/questions/43320/…
    – thomas
    Commented Nov 3, 2022 at 11:04
3

@DinoCoderSaurus is 100% correct.

When you receive input, always make sure to strip() the input, unless specified that leading and trailing spaces are required.

like this:

date = input("Date: ").strip()

see [enter link description here][1]

[1]: https://www.w3schools.com/python/ref_string_strip.asp#:~:text=The%20strip()%20method%20removes,default%20leading%20character%20to%20remove)

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)

NOT A REASON FOR BROKEN FAILED TEST: Also, try to anticipate the type of expected errors, in order to specify them in your except statement, and deal with each accordingly. e.g. ValueError

1
  • Thanks a lot! This fixed my problem immediately :) Commented Aug 25, 2022 at 10:37
0

I think if you split by " " then there must be "," in your alt_d

 try:
        alt_m, alt_d, y = date.split(" ")
        if "," in alt_d:
            for month in range(len(months)):
                if alt_m == months[month]:
                    m = month + 1
            d = alt_d.replace(",", "")

            if (int(m) > 0 and int(m) < 13) and (int(d) > 0 and int(d) < 32):
                break
-2

https://github.com/me50/Obaydullah-Hasib/blob/cs50/problems/2022/python/outdated/outdated.py you can find accepted answer here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .