Menu

Please Help Python 3 Write Program Asks User Positive Integer 0 1 Billion Exclusive Prints Q43869334

Please help in PYTHON 3

Write a program that asks the user for a positive integerbetween 0 and 1 billion, exclusive, then prints the number as atext string. For example, the number 2020 will be printed as twothousand twenty.

Background info on how to form number strings:

  • A large integer is divided into 3-digit triplets, oftenseparated 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 123million 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, andones. 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 tensand 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 asinput argument, and returns the equivalent text strings ‘twenty’ to’ninety’.

The function toTeens accepts a number in the range 10 – 19 asinput argument, and returns the equivalent text strings ‘ten’ to’nineteen’.

The function toOnes accepts a digit in the range 0 – 9 as inputargument, 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 1billion, exclusive
  • if the user input is not a number or is out of range, print anerror message
  • the user can optionally use commas in the input number. Quietlyremove the comma. You don’t have to check whether the comma is atthe right location (in between 2 triplets).

  1. Write a function toWords that will:
  • check if there is the first triplet in the number (themillions), and if it exists, call the convertTriplet function toconvert the triplet to a text string.
  • check if there is the second triplet in the number (thethousands), and if it exists, call the convertTriplet function toconvert 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 thetriplet to a text string.

  1. Write a function convertTriplet that will:
  • check if there is a hundreds value, and if it exists, call thetoOnes function to print the equivalent word for the hundreds.
  • check if there is a tens value between 20 – 90, and if itexists, call the toTens function to print the equivalent word forthe tens.
  • check if there is a teens value between 10 – 19, and if itexists, call the toTeens function to print the equivalent word forthe 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 theones.

  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 textstring
  • 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 andcall getInput again
    • If the user answers ‘n’ or ‘N’, end the program
    • If the user enters anything else, print an error message andask if they want to continue again

Additional requirements:

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

This also means no global variablesshould be used in your program.

  • Add any necessary word (such as ‘thousand’ or ‘hundred’) asneeded 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, andthey 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.
  • Don’t use recursion, use loops instead.
  • Don’t forget docstrings for each function, except main.
  • Don’t forget the beginning documentation block with your nameand short description of what the program does.

Sample output (user input in blue):

Enter a positive integer less than 1billion: 103

Number is: one hundred three

Another number? y/n: y

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

Number is: twenty-three thousand fourhundred

Another number? y/n: y

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

Number is: nine hundred ninety-ninemillion nine hundred ninety-nine thousand nine hundredninety-nine

Another number? y/n: y

Enter a positive integer less than 1billion: 1001

Number is: one thousand one

Another number? y/n: y

Enter a positive integer less than 1billion: 1234567

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

Another number? y/n: y

Enter a positive integer less than 1billion: 12a

positive integer less than 1 billiononly

Enter a positive integer less than 1billion: -5

positive integer less than 1 billiononly

Enter a positive integer less than 1billion:

positive integer less than 1 billiononly

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

positive integer less than 1 billiononly

Enter a positive integer less than 1billion: 1

Number is: one

Another number? y/n: 12

Another number? y/n: a

Another number? y/n: n

Expert Answer


Answer to Please help in PYTHON 3 Write a program that asks the user for a positive integer between 0 and 1 billion, exclusive, th…

OR