Case16. Given an integer in the range 20-69, determining the age (in years). Output a string-description of the specified age, for example: 20 - "twenty years", 32 - "thirty two years", 41 - "forty one years."

Solution in Python 3:

import random

desyatki = {
20 : 'twenty',
30 : 'thirty',
40 : 'forty',
50 : 'fifty',
60 : 'sixty'
}

edinici = {
1 : 'one',
2 : 'two',
3 : 'three',
4 : 'four',
5 : 'five',
6 : 'six',
7 : 'seven',
8 : 'eight',
9 : 'nine'
}

try:
N = random.randrange(20,70)
#N = 60
print("N = ",N)
r = N%10
print("r = ",r)
if r == 0:
print("{0} years".format(desyatki[N]))
else:
q = int(N/10)*10
print("q = ",q)
print("{0} {1} years".format(desyatki[q], edinici[r]))

except KeyError as e:
print('Error')