Reference no: EM13168155
Here are the two functions I need help with and after are two functions I wrote that will be called in these, points to the best answer
Function Name: evaluate
Parameters:
Return Value:
Description:
Write a function that will evaluate a user's expression. It should call the getExpression function that you
previously wrote to get the expression to evaluate from the user. You should evaluate the expression step-by-step.
For example, if the user inputs "7+5*8-2/2", you should first evaluate "7+5", "12*8", "96-2", and then "94/2". This
function does NOT follow the usual/correct order of operations - instead, it simply reads the expression from
left to right. THIS WILL GIVE INCORRECT OUTPUT when compared to a "Good" Calculator. You should print
the result of each step out to the screen and once the expression is completely evaluated, print out the final answer,
like so:
Step 1: 7+5 = 12
Step 2: 12*8 = 96
Step 3: 96-2 = 94
Step 4: 94/2 = 47.0
The final answer is 47.0.
You should also return the final answer as a floating point decimal.
Hints:
1. The amount of print statements needed is going to equal the total number of operators in the expression
plus one.
2. Hardcoding a check for each of the four operators may simplify this task, but you will lose 4 points; you
should use a loop so that the same code is used four times (once per each operator) for full credit.
3. Python has a built in eval function that can evaluate string expressions, but using it introduces a potential
security vulnerability in your code.
a. For example, eval("3*2") will yield 6.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function Name: solver
Parameters:
Return Value:
Description:
The solver function will act as the main function that will call the other functions described above. Once the
solver function is called, you will need to keep track of all final answers. The following will then happen in order:
1. The user will input the expression
a. If the expression is not valid (does not satisfy our conditions), the user will be prompted to enter a
valid expression.
2. Once the user enters a valid expression, you will then look into the expression to evaluate it step-by-step.
3. Once the final answer is printed out to the screen, you should ask the user whether or not they want to enter
a new expression to be solved.
a. If yes, then proceed to repeat the process of asking the user for an expression.
b. If no, do the following:
b.i. Print "Your answer(s) is/are: x, y, z..." where x, y, and z are replaced by the answers to
the user-inputted expressions.
b.ii. Print "Have a good day!"
Test Cases:
1. solver() ?
Enter an expression: 15/2/3/1
Enter an expression: 15+1
Enter an expression: 15+2-3/5*1-2
Enter an expression: 7+5*8-2/2
Step 1: 7+5 = 12
Step 2: 12*8 = 96
Step 3: 96-2 = 94
Step 4: 94/2 = 47.0
The final answer is 47.0.
Solve another expression? I don't know.
Not a valid answer, enter yes or no. Solve another expression? Yes
Enter an expression: 1+1-1+1-1
Step 1: 1+1 = 2
Step 2: 2-1 = 1
Step 3: 1+1 = 2
Step 4: 2-1 = 1
The final answer is 1.0.
Solve another expression? No.
Your answer(s) is/are: 46.0, 1.0.
Have a good day!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here is my code so far and it compiles and works
def getExpression():
#Asks the user to input a mathematical expression and checks there are 2 diff operater
userInp = input("Enter an expression: ") #prompt user and makes list
#userInpList = list(userInp)
opList = []
numList = []
#count = 0
for i in userInp: #for loop to iterate through lists
if i == "+" or i == "-" or i == "*" or i == "/": #finds operators
opList.append(i)
userInp = userInp.replace("+", " ")
userInp = userInp.replace("-", " ")
userInp = userInp.replace("*", " ")
userInp = userInp.replace("/", " ")
numList = userInp.split(" ")
#tests to see if input satisfys operator requirments
operator = len(opList)
if operator < 4:
getExpression()
if opList.count("+") > 2:
getExpression()
s
if opList.count("-") > 2:
getExpression()
if opList.count("*") > 2:
getExpression()
if opList.count("/") > 2:
getExpression()
return(numList, opList)
def getYesNo(aString):
#tests user input to see if they entered 'yes' or 'no
while True:
userInp = input(aString) #asks user question in parameter
userInp = userInp.lower() #makes lower case
if userInp == "yes":
return True
elif userInp == "no":
return False
else:
print("not a valid answer, enter yes or no.")