0
class Date:
    def __init__(self, digits):   #digits='10/20/21'
        self.month = digits[:2]  #'10'
        self.day = digits[3:5]   #'20'
        self.year = digits[6:8]  #'21'

    def __str__(self):
        return f"Dated this {self.day} day of {self.month}, 20{self.year}"

def checkday(date):        #add 'st', 'nd', 'rd', or 'th' to day
    if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
        date.day += 'st'
    elif int(date.day) == 2 or int(date.day) == 22:
        date.day += 'nd'
    elif int(date.day) == 3 or int(date.day) == 23:
        date.day += 'rd'
    else:
        date.day += 'th'

def checkmonth(date):     #get name of month
    date.month = monthdic[date.month]

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']
monthdic = {str(i): month for i, month in zip(range(1,13), months)}

date = Date(input("Enter date (mm/dd/yy):\t"))
checkday(date)
checkmonth(date)

print(date)

a couple of errors that come down to one problem that i did not think of:

if it is january: 1/12/14 will not work because self.month is 1/12/14[:2].

Enter date (mm/dd/yy):  1/12/14
Traceback (most recent call last):
  File "date.py", line 38, in <module>
    checkday(date)
  File "date.py", line 14, in checkday
    if int(date.day) == 1 or int(date.day) == 21 or int(date.day) == 31:
ValueError: invalid literal for int() with base 10: '2/'

if i resort to 01/12/14, this will also not work because 01 is '01' and monthdic['01'] does not exist:

Enter date (mm/dd/yy):  01/12/14
Traceback (most recent call last):
  File "date.py", line 39, in <module>
    checkmonth(date)
  File "date.py", line 28, in checkmonth
    date.month = monthdic[date.month]
KeyError: '01'

obviously

def __init__(self, digits):
    self.month = digits[:2]
    self.day = digits[3:5]
    self.year = digits[6:8]

is not the best approach, what are some good approaches (other than regex) ?

also one thing: would it be appropriate to call checkdate() and checkmonth inside __init__?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Aiden Choi
  • 142
  • 7
  • 3
    `self.month, self.day, self.year = map(int, digits.split("/"))`? – Samwise Jun 16 '21 at 22:56
  • 2
    `datetime.datetime.strptime()` is the canonical way to parse strings into dates. I'm not aware of any others. – wjandrea Jun 17 '21 at 01:39
  • @wjandrea no, that does not answer my question. also `if date.day.endswith('1'): date.day += 'st'` wouldn't work: `'12nd'` or `'13rd'` – Aiden Choi Jun 17 '21 at 02:00
  • @rain How doesn't it answer your question? – wjandrea Jun 17 '21 at 02:02
  • @rain Oops, I forgot about those. I deleted my comment. – wjandrea Jun 17 '21 at 02:04
  • @wjandrea i was looking for different approaches that i can have in the specific context – Aiden Choi Jun 17 '21 at 02:04
  • @wjandrea also what is up with the whitespace? every time i post on stackoverflow, there seem to be whitespace in the snippets that apparently make them not 'readable'. lol. vim has spaces to tabs enabled. not sure why the whitespace – Aiden Choi Jun 17 '21 at 02:13
  • There are also existing questions about getting ordinals, like [Display the date, like “May 5th”, using pythons strftime?](https://stackoverflow.com/q/5891555/4518341) – wjandrea Jun 17 '21 at 02:26

2 Answers2

0

The best method to use would be the split method. You can split the text that is being entered with respect to '/'. So, something like 1/12/14 would become [1, 12, 14]. The rest is straightforward.

class Date:
    def __init__(self, digits):
        split = digits.split('/')
        self.month = split[0]
        self.day = split[1]
        self.year = split[2]

    def __str__(self):
        return f"Dated this {self.day} day of {self.month}, {self.year}"
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Muhammed Jaseem
  • 782
  • 6
  • 18
0

as @MuhameedJaseem suggested: split = digits.split('/') and work with split. also improved the code as many suggested. code is now a tad bit more elegant.

class Date:
    def __init__(self, digits):
        self.month, self.day, self.year = digits.split('/')


    def __str__(self):
        return f"Dated this {self.day} day of {self.month}, {self.year}"



def checkday(date):

    if date.day[-1] == '1' and date.day[0] != '1':
        date.day += 'st'

    elif date.day[-1] == '2' and date.day[0] != '1':
        date.day += 'nd'

    elif date.day[-1] == '3' and date.day[0] != '1':
        date.day += 'rd'

    else:
        date.day += 'th'


def checkmonth(date):
    date.month = monthdic[date.month]


months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct','Nov', 'Dec']

monthdic = {str(i): month for i, month in zip(range(1,13), months)}



date = Date(input("Enter date (m/d/y):\t"))
checkday(date)
checkmonth(date)
Aiden Choi
  • 142
  • 7