0

I am doing the CS50P and am doing the outdated exercise. This is the exercise: https://cs50.harvard.edu/python/2022/psets/3/outdated/

My code does everything perfectly, except it outputs single digit dates with the zero behind the number.

Here is my code:

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

def get_date():
    while True:
        date = input("Date: ").strip().title().strip(",").replace(" ","/")
        try:
# ask user for date in month,day,year 
            m,d,y = date.split("/")            
# the input is valid if not prompt again            
# check if day is upto 31 char and year is four char
            if len(y) == 4 and int(d) >= 1 and int(d) <= 31:
# check if the first item is a letter or number               
                if m.isdigit() and int(m) >=1 and int(m) <= 12:
                    print(f"{y}-{m:02}-{d:02}")
# if it is a letter check if it corrospends to a month in the list            
                else:
                    if m.isalpha():
                        for char in month:
                            if m == char:
                                m = month.index(m) + 1
                                print(f"{y}-{m:02}-{d:02}")         
        except:
            pass
                    
get_date()

Why is it doing this.

2 Answers 2

0

For anyone facing the same problem you have to convert m and d to int for it to work. Code is still not perfect but this issue is gone.

0
  1. You are going to run into a world of hurt very shortly because you don't check that they followed a given format.
  2. Strongly suggest you don't use a list for the months and use a map. This way you don't have to use a for loop to check it and you don't have to go back and count the index either which is a poor waste of time.
  3. What happens when someone asks for "september/03,2024"? or "September 03, 2024"?
  4. Once you confirm a variable should be a number, cast it once and store the result to itself so you don't have to keep recasting it. i.e. x = int(x) now you don't have to keep casting x as an integer over and over again.
  5. Use descriptive names for your variables that match what they are doing so that your code reads naturally. avoid a bad habit now of using single character variable names. Also match plurality of variables, why call a list of months month when you could call it months? Think about someone reading the code. When you later say for char in month someone is going to think you made a mistake and have to go back and check what you are doing? Why not say for month in months? (granted you shouldn't use a for loop here at all... but thats a seperate issue)
  6. Finally without your given input we have no way to know what your output should be.

You must log in to answer this question.

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