Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "Serial/Ordinal" number conversion
- ordinal_endings = ["st", "nd", "rd", "th"]
- BASE_CHAR_POS = ord('a') - 1
- def get_last_digit(num):
- return num%10
- def get_char_pos(char):
- # Conditional expression will check whether or not char
- # is a string that needs to be converted into its ascii
- # value, or if the ascii value has already been passed.
- return (ord(char) if isinstance(char, str) else char) - BASE_CHAR_POS
- def get_ordinal(num):
- last_digit = get_last_digit(num)
- ordinal = str(num)
- ordinal_suffix = None
- # Specific cases for "teens"
- if (num > 10 and num < 21):
- ordinal_suffix = ordinal_endings[3]
- elif last_digit >= 1 and last_digit <= 3:
- ordinal_suffix = ordinal_endings[last_digit-1]
- else:
- # For numbers with last digit 0 or =/= 1,2,3
- ordinal_suffix = ordinal_endings[3]
- return ordinal + ordinal_suffix
- while True:
- try:
- # Passing input to ord is a way we can type check
- # for a character, without doing it explicitly.
- input_char_val = ord(input("Enter a character:> "))
- except:
- continue
- char_pos = get_char_pos(input_char_val)
- print("The ordinal position of this character is:", get_ordinal(char_pos))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement