Home > Software engineering >  Ordering 5 inputted dates chronologically
Ordering 5 inputted dates chronologically

Time:11-06

I need to take 5 inputted dates in the format "january 1" and order them chronologically.

one = input('birthday 1: ')
two = input('birthday 2: ')
three = input('birthday 3: ')
four = input('birthday 4: ')
five = input('birthday 5: ')
months=['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
ord_months=[]
birthday1 = one.split()                     # converts "january 1" into a list of ['january', '1']
birthday2 = two.split()
birthday3 = three.split()
birthday4 = four.split()
birthday5 = five.split()
for i in range(12):                       # range of 12 for 12 months
    if birthday1[0] == months[i]:            # calls index 0 of months, in the example above it would be january
        ord_months.append(i)              # adds the value of i to the ord_months list
for i in range(12):
    if birthday2[0] == months[i]:
        ord_months.append(i)
for i in range(12):
    if birthday3[0] == months[i]:
        ord_months.append(i)
for i in range(12):
    if birthday4[0] == months[i]:
        ord_months.append(i)
for i in range(12):
    if birthday5[0] == months[i]:
        ord_months.append(i)
ord_months.sort()
print("month:", ord_months[0] 1, 'day:', birthday1[1])
print("month:", ord_months[1] 1, 'day:', birthday2[1])
print("month:", ord_months[2] 1, 'day:', birthday3[1])
print("month:", ord_months[3] 1, 'day:', birthday4[1])
print("month:", ord_months[4] 1, 'day:', bir

I made a list of the months then i converted them into numbers then sorted the numbers. This orders the months but not the days, which i am confused about how to do.

CodePudding user response:

I suggest you rely on Python's buitin "datetime" module. The following code should do what you want above, with a lot less effort:

from datetime import datetime    
one = input('birthday 1: ')
two = input('birthday 2: ')
three = input('birthday 3: ')
four = input('birthday 4: ')
five = input('birthday 5: ')

one = datetime.strptime(one, '%B %d')
two = datetime.strptime(two, '%B %d')
three = datetime.strptime(three, '%B %d')
four = datetime.strptime(four, '%B %d')
five = datetime.strptime(five, '%B %d')

birthdays = [one, two, three, four, five]
birthdays = sorted(birthdays)

for birthday in birthdays:
    print (f"month: {birthday.strftime('%B')}, day: {birthday.strftime('%d')}")

You can find out more about datetime here:https://docs.python.org/3/library/datetime.html#, with specifics around what "strptime" and "strftime" are doing here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

You can also simplify this (and extend its functionality) like this:

from datetime import datetime

n_birthdays = int(input('Number of birthdays to ask for: '))

birthdays = []

for n in range (n_birthdays):
    birthdays.append(datetime.strptime((input(f'birthday {n 1}: ')), '%B %d'))

birthdays = sorted(birthdays)

for birthday in birthdays:
    print (f"month: {birthday.strftime('%B')}, day: {birthday.strftime('%d')}")
  • Related