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

  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.

  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.

  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.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  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.

  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.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

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