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

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  A string is valid windows filename.

writing a function in python that verifies whether a string is valid windows filename.

  Project will be a simple, working program

This programming project will be a simple, working program, using Python language, which utilizes a good design process and includes:Sequential, selection, and repetitive programming statements as well as,At least one function call.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  The number of lowercase letters in the file

The number of uppercase letters in the file The number of lowercase letters in the file

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Same directory as your program

In the same directory as your program, create a file FF1, and write into it Hello (with a space at teh end). Similarly, create a file FF2, and write into it world! (with a new line, i.e., an ENTER at the end). And create a file DD and write into i..

  Code for the haunted house game

Improve the game by adding more features, for example you can examine more items, more props etc. You may implement this using more lists regarding items and props, remember, you should check if the object is being carried or in the location of th..

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

  Program that allows the user to enter the type of coin

Create a program that allows the user to enter the type of coin to be evaluated and the number of coins. The program should calculate the total weight of the coins and the height of the coins when stacked. Name the program coins.py. Use the table bel..

  Function should return a dictionary

Write a function numOccur(s), where s is a string; the function should return a dictionary whose keys are the 26 ascii letters abcdefghijklmnopqrstuvwxyz

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

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