Please I need help . I'm facing the same problem and don't know what to do.
Any help is appreciated.
I have this error message:
https://submit.cs50.io/check50/920b5463f68b374c1e6ab130b8041766f9eeb99c
This was my code :
import re
def main():
month_dict = {1:"January",2:"February",3:"March",4:"April",
5:"May",6:"June",7:"July",8:"August",
9:"September",10:"October",11:"November",12:"December"}
while True:
date = input("Date: ").strip()
try:
# Replace -, /, , with space
for sep in ['-', '/', ',']:
date = date.replace(sep, ' ')
# get rid of white spaces
list_date = [p for p in date.split() if p]
# Numeric format for Month
if list_date[0].isdigit():
month = int(list_date[0])
day = int(list_date[1])
year = int(list_date[2])
# Month-name format
else:
month_name = list_date[0]
month = None
for k, v in month_dict.items():
if month_name.lower() == v.lower():
month = k
if month is None:
raise ValueError("Invalid month name.")
day = int(list_date[1])
year = int(list_date[2])
# Make sure the range of months and days is correct
if not (1 <= month <= 12):
raise ValueError("Month must be between 1 and 12.")
if not (1 <= day <= 31):
raise ValueError("Day must be between 1 and 31.")
# Format as YYYY-MM-DD
new_date = f"{year}-{month:02}-{day:02}"
print(new_date)
break # exit the loop if everything is correct
# prompt the user to enter a correct date
except Exception as e:
print(f"Error: {e}")
print("Please try again with a valid date format like 9/5/2020 or September 8, 1636.")
if __name__ == "__main__":
main()