Determine Century

Take a year and print the corresponding century (e.g., "19th century").

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 9 of 10
determine-century.py
1# Take year
2year = int(input("Enter a year: "))
3
4# Calculate century
5if year % 100 == 0:
6 century = year // 100
7else:
8 century = (year // 100) + 1
9
10# Format century
11if century == 1:
12 suffix = "st"
13elif century == 2:
14 suffix = "nd"
15elif century == 3:
16 suffix = "rd"
17else:
18 suffix = "th"
19
20print(f"{century}{suffix} century")

Output

Enter a year: 2024
21st century

Enter a year: 1900
19th century

Enter a year: 1950
20th century

What's going on

Calculate century from year.

Key Concepts:

If year divisible by 100: century = year/100
Otherwise: century = (year/100) + 1
Add appropriate suffix (st, nd, rd, th)