Evaluate a user''s expression

Assignment Help Python Programming
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:

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

Reference no: EM13168155

Questions Cloud

Design a modified priority encoder : Design a modified priority encoder that receives an 8-bit input, A7:0, and produces two 3-bit outputsm Y2:0 and Z 2:0 Y indicates the most significant bit of the input that is TRUE
Dimensional array of integers and fill it with data : Create a 2-by-3 two-dimensional array of integers and fill it with data. Loop through the array and locate the smallest value stored. Print out the smallest value as well as its row and column position in the array
What is the role of a dbms : What is the role of a DBMS, and what are it advantages? What are its disadvantages?
Decimal digit in bcd : Design a combinational circuit with four input lines that represent a decimal digit in BCD and four output lines that generate the 9's complement of the input digit.
Evaluate a user''s expression : 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.
Calculate the celsius equivalent of a fahrenheit temperature : Construct a program that allows you to calculate the Celsius equivalent of a Fahrenheit temperature.
Create an application in which a user can enter a phone book : Create an application in which a user can enter a phone book entry, including the following elements: First Name, Last Name, Phone Number, email address
The contenders are tortoise and hare : The contenders are Tortoise and Hare, and they begin race as investors at "tile 1" of 70 tiles The finish line is at 70 the tile. With each tick of the clock
The mips architecture reserves register : The MIPS architecture reserves register 0 (called $zero) to be always equal to 0. This allows synthesizing additional addressing modes and additional instructions from the instruction set.

Reviews

Write a Review

Python Programming Questions & Answers

  The program should allow the student

The program should allow the student to enter the answer. If the answer is correct, a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Python program that reads in a series of positive integers

Write a Python program that reads in a series of positive integers and writes out theproduct of all the integers less than 25 and the sum of all the integers greater than or equal to 25. Use 0 as a sentinel value

  Display the percent-ages of the overall grade

Write a program that uses a bar chart to display the percent-ages of the overall grade represented by the project, quizzes the midterm exam and the final exam

  Write python code that will execute a list

Write python code that will execute a list of functions with supplied parameters and report the observed runtime for each function run. Assume that the input file has a list of strings like so:

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Fill in the python code

Fill in the Python code to play Tic Tac Toe. I won't award points unless it runs succesfully. # Tic-Tac-Toe Game def drawBoard(board): # Draws the board using the list of numbers print(" ") print(" ",board[0]," | ",board[1]," | ", board[2]) print("--..

  Data file is a comma separated

The data file is a comma separated text values stored in a file with '.CSV' extension. The file has five columns corresponding to employee data fields listed above.

  Cleint software so that it does not display

Rewrite the cleint software so that it does not display an echo of a message sent by the users. Maybe it means that each time a user tries to send a private message that same message is also sent back to them? summary: stop that from happening?

  Define three types of programming errors

Define three types of programming errors and explain with examples

  Define a function to calculate the values

Be sure to define a function to calculate the values and print the table displaying the interest rate in the first column, the monthly payment in the second column, and the total payment in the third column. Your program should not allow the user to ..

  Assume an n × n matrix a is given

Assume an n × n matrix A is given, containing only 1's and 0's, such that, in each row, all 1's come before all 0's. Give an O(n log n) algorithm to count all 1's in A.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd