In: Computer Science
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’.
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")