Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

PLEASE HELP IN PYTHON 3 Write a program that asks the user for a positive integer between 0 and 1 billion, exclusive, then prints the

PLEASE HELP IN PYTHON 3

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 2020 will be printed as two thousand twenty.

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:
    • 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
    • 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 lab2.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'.

WHICH IS PROVIDED HERE:

# Lab 2: print an integer as a text string

def toOnes(digit) :

''' Turn a digit into an equivalent word '''

if digit == 1 : return "one"

if digit == 2 : return "two"

if digit == 3 : return "three"

if digit == 4 : return "four"

if digit == 5 : return "five"

if digit == 6 : return "six"

if digit == 7 : return "seven"

if digit == 8 : return "eight"

if digit == 9 : return "nine"

return ""

def toTeens(number) :

''' Turn a number between 10 and 19 into an equivalent word '''

if number == 10 : return "ten"

if number == 11 : return "eleven"

if number == 12 : return "twelve"

if number == 13 : return "thirteen"

if number == 14 : return "fourteen"

if number == 15 : return "fifteen"

if number == 16 : return "sixteen"

if number == 17 : return "seventeen"

if number == 18 : return "eighteen"

if number == 19 : return "nineteen"

return ""

def toTens(number) :

''' Turn a number between 20 and 99 into an equivalent word '''

if number >= 90 : return "ninety"

if number >= 80 : return "eighty"

if number >= 70 : return "seventy"

if number >= 60 : return "sixty"

if number >= 50 : return "fifty"

if number >= 40 : return "forty"

if number >= 30 : return "thirty"

if number >= 20 : return "twenty"

return ""

Follow these steps to complete the lab assignment:

  1. 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 a number or is out of range, print an error message
  • the user can optionally use commas in the input number. Quietly remove the comma. You don't have to check whether the comma is at the right location (in between 2 triplets).

  1. 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 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 triplet to a text string.
  • check if there is the third triplet in the number (the ones), and if exists, call the convertTriplet function to convert the triplet to a text string.

  1. Write a function convertTriplet that will:
  • check if there is a hundreds value, and if it exists, call the toOnes function to print the equivalent word for the hundreds.
  • check if there is a tens value between 20 - 90, and if it exists, call the toTens function to print the equivalent word for the tens.
  • check if there is a teens value between 10 - 19, and if it exists, call the toTeens function to print the equivalent word for the teens.
  • check if there is a ones value between 0 - 9, and if it exists, call the toOnes function to print the equivalent word for the ones.

  1. Write a main function that will:
  • call the getInput function to get a valid integer
  • call the toWords function to convert the number to a text string
  • print the text string
  • ask the user if they want to continue
    • if the user answers 'y' or 'Y', go back to the first step and call getInput again
    • If the user answers 'n' or 'N', end the program
    • If the user enters anything else, print an error message and ask if they want to continue again

Additional requirements:

  • The program must have the 4 functions as outlined above, and each should do their tasks shown above.
  • There is no description of input arguments or return values for each function. It's up to you to decide what data to pass to the functions and what data to return from the functions.

This also means no global variables should be used in your program.

  • Add any necessary word (such as 'thousand' or 'hundred') as needed to the output text string.
  • Words in the output text string should be separated by 1 space. They should not be jammed together with no space in between, and they should not be separated by more than 1 space.
  • You should not have to use any Python data structures (list, etc.) or any Python module that we haven't covered.
  • Dont use recursion, use loops instead.
  • Don't forget docstrings for each function, except main.
  • Don't forget the beginning documentation block with your name and short description of what the program does.

Sample output (user input in blue):

Enter a positive integer less than 1 billion: 103

Number is: one hundred three

Another number? y/n: y

Enter a positive integer less than 1 billion: 23,400

Number is: twenty-three thousand four hundred

Another number? y/n: y

Enter a positive integer less than 1 billion: 999,999,999

Number is: nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine

Another number? y/n: y

Enter a positive integer less than 1 billion: 1001

Number is: one thousand one

Another number? y/n: y

Enter a positive integer less than 1 billion: 1234567

Number is: one million two hundred thirty-four thousand five hundred sixty-seven

Another number? y/n: y

Enter a positive integer less than 1 billion: 12a

positive integer less than 1 billion only

Enter a positive integer less than 1 billion: -5

positive integer less than 1 billion only

Enter a positive integer less than 1 billion:

positive integer less than 1 billion only

Enter a positive integer less than 1 billion: 123,456,789,012

positive integer less than 1 billion only

Enter a positive integer less than 1 billion: 1

Number is: one

Another number? y/n: 12

Another number? y/n: a

Another number? y/n: n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

How do you figure out the balance to a ledger T account?

Answered: 1 week ago