Python Programming Exercises, Gently Explained
136
Alternate Solution:
def ordinalSuffix(number):
# 11, 12, and 13 have the suffix th:
if number % 100 in (11, 12, 13):
return str(number) + 'th'
# Numbers that end with 1 have the suffix st:
if number % 10 == 1:
return str(number) + 'st'
# Numbers that end with 2 have the suffix nd:
if number % 10 == 2:
return str(number) + 'nd'
# Numbers that end with 3 have the suffix rd:
if number % 10 == 3:
return str(number) + 'rd'
# All other numbers end with th:
return str(number) + 'th'
Dostları ilə paylaş: