In: Computer Science
Write a function named int2ordinal that takes an integer as its only parameter and returns the number with its appropriate suffix as its result (stored in a string). For example, if your function is passed the integer 1 then it should return the string "1st". If it is passed the integer 12 then it should return the string "12th". If it is passed 2003 then it should return the string "2003rd". Your function must not print anything on the screen.
You can use the remainder operator to extract the last digit of an integer by computing the remainder when the integer is divided by 10. Similarly, you can extract the last two digits of an integer by computing the remainder when the integer is divided by 100. For example 29 % 10 is 9 while 1911 % 100 is 11. Then you can construct the string that needs to be returned by your function by converting the integer parameter into a string by calling the str function, and concatenating the appropriate suffix using the + operator.
REQUIRED CODE IS :
def int2ordinal(num):
num = str(num)
if len(num) > 2: # Will be a year, not necessarily consisting of four digits. Could be 333
end_digits = int(num) % 100
else: # Will be month / day
end_digits = int(num) % 10
if end_digits == 1: return (num + "st")
if end_digits == 2: return (num + "nd")
if end_digits == 3: return (num + "rd")
else: return (num + "th")
def main():
day = input("Enter a day between 1 and 31: ")
month = input("Enter a month between 1 and 12: ")
year = input("Enter a year between 1 and 2100: ")
print("On the", int2ordinal(day), "day of the", int2ordinal(month), \
"month of the", int2ordinal(year), "year, I got my degree")
main()
CUSTOM INPUT :
1
3
2013
OUTPUT :
'On the', '1st', 'day of the', '3rd', 'month of the', '2013th', 'year, I got my degree'

