Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that asks the user for a positive integer between 0 and 1 billion, exclusive, then prints the number as a text string.

Write a program that asks the user for a positive integer between 0 and 1 billion, exclusive, then prints the number as a
text string. For example, the number 2,024 will be printed as: two thousand twenty-four.
Background info on how to form number strings:
A large integer is divided into 3-digit triplets, often separated by a comma. For example: 12,078
For a number up to 1 billion there are up to 3 triplets: millions, thousands, and ones. For example: 123,456,789 has
123 million as the first triplet, 456 thousand as the second triplet, and 789 as the third triplet.
For each triplet there are 3 place values: hundreds, tens, and ones. For example: 235 has 2 hundreds, 3 tens, 5 ones.
The tens are divided into 2 types:
o From 20-90, the tens text strings are twenty, thirty, forty... and can optionally go with a ones value.
If there is a ones value, then there is a dash in between the tens and ones values. For example: twenty-three
o From 10-19, the text strings are ten, eleven, twelve, thirteen... and there is no ones value because it's already
'built-in'
Download the file convert.py, which has 3 convert functions: toTens, toTeens, and toOnes
The function toTens accepts a number in the range 20-90 as input argument, and returns the equivalent text strings
'twenty' to 'ninety'.
The function toTeens accepts a number in the range 10-19 as input argument, and returns the equivalent text strings
'ten' to 'nineteen'.
The function toOnes accepts a digit in the range 0-9 as input argument, and returns the equivalent text strings 'one' to
'nine'.
Follow these steps to complete the lab assignment:
1. At the top of your source file, import the convert.py module with: import convert
(Note that theres no .py in the import statement)
2. Write a function getInput that will:
ask the user for a positive integer under 1 billion.
keep prompting until you get an integer between 0 and 1 billion, exclusive
if the user input is not an integer or is out of range, print an error message
the user can optionally use commas in the input number. Quietly remove the comma (which means no printing
to screen that the comma is removed). You don't have to check whether the comma is at the right location (in
between 2 triplets).
3. Write a function toWords that will:
check if there is the first triplet in the number (the millions), and if it exists, call the convertTriplet function to
convert the millions triplet to a text string.
check if there is the second triplet in the number (the thousands), and if it exists, call the convertTriplet function
to convert the thousands triplet to a text string.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

Students also viewed these Databases questions