Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The folloeing program is in python. Please see my attched code. I have it mostly working. Download the file lab4.py, which is the program intName.py

The folloeing program is in python. Please see my attched code. I have it mostly working.

Download the file lab4.py, which is the program intName.py from the book. The intName.py program reads in a positive integer and prints out the English name for the number. For example, if the input integer is 355, then the program prints "three hundred fifty five".

Follow these steps to complete the lab assignment:

Read and run lab4.py so you understand what it does. A good programmer can read code as well as write code. The program has 4 functions and a main function.

Note that the output text string has a space in front, for example, " twenty". Modify the code so that there's no space when the text string is printed. Your code change for this step should not be in the main function.

To use proper English, any value between 20 and 100 that has a non-zero last digit should have a hyphen between the 2 words, for example, "thirty-one" or "sixty-seven". Modify the code (not in main) so that the hyphen is printed for these cases.

Find the right location to add code so that the input integer can be as large as 999,999. Don't forget requirement 3 as you expand to the thousands digits.

Add code to the main function so that it keeps prompting the user for a positive integer between 0 and 1,000,000 and printing the output text string. If the user enters an invalid value, print an error message and re-prompt. The program ends when the user enters 0.

Test your program with all of the following output or their equivalent values.

Sample program output (user input in blue):

Please enter a positive integer < 1,000,000 (or 0 to end): -3

input must be between 0 and 1,000,000

Please enter a positive integer < 1,000,000 (or 0 to end): 1000

one thousand

Please enter a positive integer < 1,000,000 (or 0 to end): 987654

nine hundred eighty-seven thousand six hundred fifty-four

Please enter a positive integer < 1,000,000 (or 0 to end): 300003

three hundred thousand three

Please enter a positive integer < 1,000,000 (or 0 to end): 300300

three hundred thousand three hundred

Please enter a positive integer < 1,000,000 (or 0 to end): 300333

three hundred thousand three hundred thirty-three

Please enter a positive integer < 1,000,000 (or 0 to end): 333444

three hundred thirty-three thousand four hundred forty-four

Please enter a positive integer < 1,000,000 (or 0 to end): 333404

three hundred thirty-three thousand four hundred four

Please enter a positive integer < 1,000,000 (or 0 to end): 12

twelve

Please enter a positive integer < 1,000,000 (or 0 to end): 10001

ten thousand one

Please enter a positive integer < 1,000,000 (or 0 to end): 2000000

input must be between 0 and 1,000,000

Please enter a positive integer < 1,000,000 (or 0 to end): 0

Just for fun, here's a website that does the same task as your program, but it doesn't work with numbers as large as your program: http://researchmaniacs.com/Numbers/Spell/How-to-spell-the-number-1.html And, if you've ever received a check that's computer generated, the code to print the amount of money is the code that you're writing.

MY CODE:

def intName(number) : ''' Turn a number into its English name @param: number (int) @return: the name (str) ''' part = number # The part that still needs to be converted. name = "" # The name of the number. if part >= 10000 :#THis part is not working...... name += tensName(part // 1000) + " thousand " part = part % 1000 print("Test 1", part) if part >= 1000 :#works up to 9,999 name += digitName(part // 1000) + " thousand " part = part % 1000 print("Test 2", part) if part >= 100 : name += digitName(part // 100) + " hundered" part = part % 100

if part >= 20 : name = name + " " + tensName(part) #Changed print("Test 3", part) if part > 20: name = name + "-" #adds the "- between part = part % 10 elif part >= 10 : name = name + " " + teenName(part) part = 0 print("Test 4", part)

if part > 0 : name = name + "" + digitName(part) print(part) name = name.strip()#removes all trailing and leading whitespace return name #PART 3 goes in here too!

def digitName(digit) : ''' Turn a digit into its English name @param: digit (int) @return: name (str) ''' 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 teenName(number) : ''' Turn a number between 10 and 19 into its English name @param: number (int) @return: name (str) ''' 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 tensName(number) : ''' Turn a number between 20 and 99 into its English name @param: number (int) @return: name (str) ''' 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 ""

def main() : value = int(input("Please enter a positive integer < 1000: ")) print(intName(value)) # Start the program. main()

Thanks! Please modify the existing code above......

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

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago