1

Here are the instructions I received for my assignment:

4) Add the following function into Mylib

scalc(p1)
p1 will be a string like this "N1, N2, operator"
examples
scalc("20,30,*")
the result will be 600

scalc("50,20,+")
the result will be 70

scalc("50,20,-")
the result will be 30

scalc("60,20,/")
the result will be 30

use string functions to parse the first number, the second number, and the operator from the input string. use the prior functions (add, subtract, divide and multiply ) to do the calculations.


And here is my attempt that is not working. I have the add() sub() mult() div() functions, I'm just not showing them here.

I know it's something very simple that likely has to do with where I call the function scalc(p1). What is the proper way to do that?

def scalc(p1):
    astring = p1.split(",")
    num1 = float(astring[0])
    num2 = float(astring[1])
    if astring[3] == "+":
        add()
    elif astring[3] == "-":
        sub()
    elif astring[3] == "*":
        mult()
    elif astring[3] == "/":
        div()
    return num1, num2

p1 = input("Enter two numbers and an operator, each separated by a comma: ")
scalc(p1)

EDIT: Here is the Answer. I did not have arguments being passed to my functions. By adding num1 and num2 to every instance of my arithmetic functions, they were able to receive the new variable values.

#Define the main program function
def main():
    #Define input function
    def float_input(msg):
        while True:
            try:
                return float(input(msg))
            except ValueError:
                print("You must enter a number!")
            else:
                break
    #Declare variables        
    rangeLower = float_input("Enter your Lower range: ")
    rangeHigher = float_input("Enter your Higher range: ")
    num1 = float_input("Enter your First number: ")
    num2 = float_input("Enter your Second number: ")

    #Define formula functions
    def add(num1, num2):
        sum = num1 + num2
        print("The Result of",num1,"+",num2,"=", sum)

    def sub(num1, num2): 
        diff = num1 - num2
        print("The Result of",num1,"-",num2,"=", diff)

    def mult(num1, num2):
        product = num1 * num2
        print("The Result of",num1,"*",num2,"=", product)

    def div(num1, num2):
        if num2 == 0:
            print("The Result of",num1,"/",num2,"= You cannot divide by Zero")
        else:
            quotient = num1 / num2
            print("The Result of",num1,"/",num2,"=", quotient)

    #If-else
    if num1 < rangeLower or num1 > rangeHigher or num2 < rangeLower or num2 > rangeHigher:
        print("The input values are outside the input ranges.")
        print("Please check the number and try again.")
        print("Thanks for using our calculator")
    else:
        #Call functions
        add(num1, num2)
        sub(num1, num2)
        mult(num1, num2)
        div(num1, num2)
        print("Thanks for using this calculator!")

    def scalc(p1):
        astring = p1.split(",")
        num1 = float(astring[0])
        num2 = float(astring[1])
        if astring[2] == "+":
            add(num1, num2)
        elif astring[2] == "-":
            sub(num1, num2)
        elif astring[2] == "*":
            mult(num1, num2)
        elif astring[2] == "/":
            div(num1, num2)
        return num1, num2

    p1 = input("Enter two numbers and an operator, each separated by a comma: ")
    scalc(p1)
1
  • 2
    You're not passing any values to your functions (you should have included at least one in your question!), nor are you capturing the result. add() should presumably be result = add(num1, num2) Commented Dec 8, 2019 at 23:42

1 Answer 1

2

This does it. There were a couple errors. First, in Python you start counting at 0, so you wanted to use astring[2] instead of astring[3]. Also you needed a value to be returned:

def scalc(p1):
    astring = p1.split(",")
    print(astring)
    num1 = float(astring[0])
    num2 = float(astring[1])
    if astring[2] == "+":
        add(num1,num2)
    elif astring[2] == "-":
       sub(num1,num2)
    elif astring[2] == "*":
        mult(num1,num2)
    elif astring[2] == "/":
        div(num1,num2)
    return value

p1 = input("Enter two numbers and an operator, each separated by a comma: ")
scalc(p1)

Example:

input: "20,30,+"

Out[2]: 50.0
Sign up to request clarification or add additional context in comments.

7 Comments

Oh! Okay I see I had 3 instead of 2. I fixed that, but now I still need to replace num1 and num2 in the original code with the new values.
Honestly, I don't even know if we need to keep the original code except for the formula functions. I could just delete the rest of the code where num1 and num2 are originally given values and use the new function we were given.
Okay, so you added the formulas for add, substract, divide, multiply... but I already had those formulas in functions built from previous assignments. We were told to use our existing functions. So instead of returning the value, I need to return num1 and num2 so that my other functions can use them. I updated the code in the original post.
So, right now that I changed my 3 to a 2, the code runs; however, calling one of my formula functions returns the original num1 and num2 from the previous input.
You're missing arguments in your functions, you should consider adding them so your main function is functional. Because this is homework I shouldn't help your further, but should be enough for you to find the right way
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.