Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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:

  • None

Return Value:

  • A float

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:

  • None

Return Value:

  • None

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.")

Python, Programming

  • Category:- Python
  • Reference No.:- M9458271

Have any Question?


Related Questions in Python

Question a software company sells a package that retails

Question : A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program usi ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

Environment setupthe first mini project will be based on

Environment Setup The first mini project will be based on Ladder Logic programming. We will be using Schneider Electric's IDE called SoMachine Basic to do the programming. The latest ver- sion of SoMachine Basic for Wind ...

Question write a simple python program that takes use

Question: Write a simple python program that takes use inputs as non-zero digits and converts them into binary form. The response must be typed, single spaced, must be in times new roman font (size 12) and must follow th ...

Question why is software configuration management

Question : Why is software configuration management considered an umbrella activity in software engineering? Please include examples and supporting discussion. The response must be typed, single spaced, must be in times ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Part i the assignment filesone of the most important

Part I: The Assignment Files One of the most important outcomes of this assignment is that you understand the importance of testing. This assignment will follow an iterative development cycle. That means you will write a ...

Question write a python program with a graphical user

Question: Write a python program with a graphical user interface that will allow a user to create a custom pizza which they wish to order. At minimum, the user should be able to choose the size of the pizza, the type of ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As