Created
November 29, 2024 16:10
-
-
Save jswright61/713e41b2c2b32cd746d884f395532b2e to your computer and use it in GitHub Desktop.
Ordinalize an integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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