In: Computer Science
Objective: Work with basic input commands. Work with decision making commands. Assignment This will be done using Python. Part 1 Create a program that asks the user to input a number between 20 and 99. The program must then print out the number in English words. If the user does not enter a value between 20 and 99 display an error message stating that the input is not within a valid range. No credit will be given for programs that have a separate print statement for each value. Example: Please enter a value: 42 The number is forty two. (running the program again) Please enter a value: 30 The number is thirty. (running the program again) Please enter a value: 10 The number is not within a valid range.
Below is the required program. It is well explained inside the code using comments.
# read number
num = int(input('Please enter a value: '))
# check if number is invalid
# and display the error message
if num < 20 or num > 99:
print('The number is not within a valid
range.')
else:
# declare list having english words of ones
place digits
onesName =
['','one','two','three','four','five','six','seven','eight','nine']
# declare list having english words
of tens place digits
tensName =
['','','twenty','thirty','forty','fify','sixty','seventy','eighty','ninety']
# extract both digits
tens = int(num / 10)
ones = int(num % 10)
# get the corresponding words from
the list, and print
print('The number is', tensName[tens],
onesName[ones])
Below is the screenshot of the program for indentation:
Below is the sample output:
This completes the requirement. Let me know if you have any questions.
Thanks!