100 days of code: Day 13 - if elif else

Last modified on 2020-06-21

Source: 100-days-of-code.

Building on yesterday’s play with conditional statements, I made a program - ordinal_numbers.py - that uses an if statement to print a number a certain way depending on what the number is …

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

for number in numbers:
    if number == 1:
        print(str(number) + "st")
    elif number == 2:
        print(str(number) + "nd")
    elif number == 3:
        print(str(number) + "rd")
    else:
        print(str(number) + "th")

Run …

$ ./ordinal_numbers.py 
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th

Happy hacking!