[Python] CS50P PSET 3 "Outdated": I am failing one testcase

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attempting this problem: https://cs50.harvard.edu/python/2022/psets/3/outdated/ which is basically a program that takes input in "month/day/year" format or "month day, year" format and outputs in YYYY-MM-DD format. After running my code through check50 (a program that tests your code), I passed all the conditions except 1: "input of September 8 1636 results in reprompt" Do I make the program require the "," be in the date?

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

    while True:
    try:
    date = input("Date: ")
    month, day, year = date.split("/")
    if (int(month)>=1 and int(month)<=12) and ( 1 <= int(day) <= 31):
    day = int(day)
    if day < 10:
    day = f"{int(day):02}"
    if int(month) <10:
    month = f"{int(month):02}"
    break
    except(ValueError):
    try:
    month, day, year = date.split(" ")
    day = day.strip(",")
    if month in months and ( 1 <= int(day) <= 31):
    for m in months:
    if m == month:
    month = months.index(m) + 1
    month = f"{int(month):02}"
    day = f"{int(day):02}"
    break
    except:
    pass
    print(f"{year}-{month}-{day}")

    Continue reading...

Compartilhe esta Página