Question

In: Computer Science

This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter...

This problem requires python(Project: Evaluate Word Problems) Write a script that enables the user to enter mathematical word problems like "two times three" and " seven minus five", then use string processing to break apart the string into the numbers and the operation and return the result. So “two times three” would return 6 and “seven minus five would return 2. To keep things simple, assume the user enters only the words for the numbers 0 through 9 and only the operations ‘ plus’, ‘minus’, ‘times’, and ‘divided by’.

Solutions

Expert Solution

n = input("Enter the arithmetic operation to do:").strip().split(" ")
d = {'zero':0,'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9}
try:
    if(n[1].strip().lower()=='plus' or n[1].strip().lower()=='minus' or n[1].strip().lower()=='times' or n[1].strip().lower()=='divided'
    or n[1].strip().lower()=='dividedby'):
        if len(n) == 3:
            value1 = d[n[0].strip().lower()]
            value2 = d[n[2].strip().lower()]
            if n[1].strip().lower() == 'plus':
                print(value1+value2)
            elif n[1].strip().lower() == 'minus':
                print(value1 - value2)
            elif n[1].strip().lower() == 'times':
                print(value1 * value2)
            elif n[1].strip().lower() == 'dividedby':
                print(value1 / value2)
            else:
                print("Enter 0 to 9 value in form of digits")
                print("Operators are")
                print("plus\n minus\n times\n divided by")

        elif len(n) == 4:
            value1 = d[n[0].strip().lower()]
            value2 = d[n[3].strip().lower()]
            if n[1].strip().lower()=='divided' and n[2].strip().lower()=='by':
                print(value1/value2)
            else:
                print("Enter 0 to 9 value in form of digits")
                print("Operators are")
                print("plus\n minus\n times\n divided by")
        else:
            print("Enter 0 to 9 value in form of digits")
            print("Operators are")
            print("plus\n minus\n times\n divided by")
    else:
        print("Enter 0 to 9 value in form of digits")
        print("Operators are")
        print("plus\n minus\n times\n divided by")
except IndexError:
    print("Enter 0 to 9 value in form of digits")
    print("Operators are")
    print("plus\n minus\n times\n divided by")
except KeyError:
    print("Enter 0 to 9 value in form of digits")
    print("Operators are")
    print("plus\n minus\n times\n divided by")

Related Solutions

Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
5) Write a JavaScript script that will ask the user to enter in the length of...
5) Write a JavaScript script that will ask the user to enter in the length of the side of a cube. (all the sides of a cube are equal) Based on the value that is entered by the user from the keyboard, calculate each of the following and assign your results to variables. 1. volume of the cube 2. surface area of the cube 3. volume of the largest sphere that could fit in the box. 4. surface area of...
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function...
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function that will receive the word, use the word to compile a new word in Robbery language and return the new Robbery language word. In the function: Use a while loop and a newline character (‘\0’) to step through the characters in the original string. How to compile a word in Robbery language: Double each consonant and put the letter ‘o’ between the two consonants....
Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
In python, write a program that asked the user to enter the monthly costs for the...
In python, write a program that asked the user to enter the monthly costs for the following expenses incurred from operating his automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should display total monthly cost and annual cost. Also, should contain main and calcExpenses functions, calcExpenses should only calculate the expenses and everything else should happen in the main function.
Write a C++ script to perform the following: a.) Ask a user to enter 2 real...
Write a C++ script to perform the following: a.) Ask a user to enter 2 real numbers. b.) Save the user response as a variable a and b. c.) Display if the numbers are even or odd. d.) Check if the number a belongs in the interval D = [10, 99], display the output as "a is between 10 and 99" or "a is not between 10 and 99". e.) Check if the number b belongs in the interval E...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT