Search code examples
pythonexceptioncs50valueerror

Always have a ValueError, how to fix it?


I'm learning CS50P and I meet some troubles when sloving problem sets. I enter 9/8/1636 but the output shows there is ValueError. I attached both the question and my code, and I us VS code. Hope someone can do me a favour.

Question: In the United States, dates are typically formatted in month-day-year order (MM/DD/YYYY), otherwise known as middle-endian order, which is arguably bad design. Dates in that format can’t be easily sorted because the date’s year comes last instead of first. Try sorting, for instance, 2/2/1800, 3/3/1900, and 1/1/2000 chronologically in any program (e.g., a spreadsheet). Dates in that format are also ambiguous. Harvard was founded on September 8, 1636, but 9/8/1636 could also be interpreted as August 9, 1636! Fortunately, computers tend to use ISO 8601, an international standard that prescribes that dates should be formatted in year-month-day (YYYY-MM-DD) order, no matter the country, formatting years with four digits, months with two digits, and days with two digits, “padding” each with leading zeroes as needed. In a file called outdated.py, implement a program that prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636, wherein the month in the latter might be any of the values in the list below: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] Then output that same date in YYYY-MM-DD format. If the user’s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has 28, 29, 30, or 31 days.

I asked GPT-4 for help but in vain. I really don't know what to do. Please help me.

month_list = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]
while True:
    try:
        date = input('Please enter a date in month-day-year order: ').replace(' ','/').replace(',','/')
        date_list = list(date.split('/'))
        month = date_list[0]
        day = date_list[1]
        year = date_list[2]

        if month in month_list:
            month = month_list.index(month) + 1
        elif month.isdigit() and 1<=int(month)<=12:
            month=int(month)
        else:
            print('Month should be a valid month name or a number between 1 and 12.')
            continue

        if 1<=int(day)<=31:
                print(f'{year}-{month:02d}-{day:02d}')
        else:
            print('Day should be between 1 and 31.')
            continue
    
    except ValueError:
        print('Date invalid, please enter again.')

Solution

  • Try to replace print(f'{year}-{month:02d}-{month:02d}') with print(f'{year}-{int(month):02d}-{int(month):02d}').

    The formatter 02d expects an integer, not a string, see this answer: https://stackoverflow.com/a/36543850/18189622

    In general I would advise against using try and except as broadly as you did, or at least to remove it or comment it out for debugging, then you will get error messages showing more clearly which lines are causing the error.