Skip to content

Instantly share code, notes, and snippets.

@jswright61
Created November 29, 2024 16:10
Show Gist options
  • Select an option

  • Save jswright61/713e41b2c2b32cd746d884f395532b2e to your computer and use it in GitHub Desktop.

Select an option

Save jswright61/713e41b2c2b32cd746d884f395532b2e to your computer and use it in GitHub Desktop.
Ordinalize an integer
#!/opt/homebrew/bin/python3
import sys
def ordinalize(n):
if n % 100 in (11, 12, 13):
return f"{n}th"
else:
match n % 10:
case 1:
return f"{n}st"
case 2:
return f"{n}nd"
case 3:
return f"{n}rd"
case _:
return f"{n}th"
try:
num = int(sys.argv[1])
except IndexError:
print("You must supply an integer as a commandline argument")
exit(1)
except ValueError:
print("You must supply a valid integer")
exit(1)
print(ordinalize(num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment