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...
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 Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
Write a program that prompts the user enter a word, and determines the first half and...
Write a program that prompts the user enter a word, and determines the first half and second half of the word. Print the first and second half of the word on different lines. Sample runs of the program are shown below. Note: If the word is an odd number of letters, the first half should contain fewer letters (as shown in sample run #2) SAMPLE PROGRAM RUN #1                     Enter a word: carrot First half: car Second half: rot SAMPLE PROGRAM...
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT