Search code examples
pythonpython-module

How to input string to function?


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)

Solution

  • 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