Python Forum
Help!!!! Error in Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help!!!! Error in Code (/thread-42117.html)



Help!!!! Error in Code - Ravi1001 - May-13-2024

Hi,

I am working on a calculator.

I somehow completed it, but i am getting an error.
Unable to figure out.

Help!

Thanks!

# Add
def add (n1, n2):
    return n1 + n2 

# substract
def subtract(n1, n2):
    return n1 - n2 

# multiply
def multiply (n1, n2):
    return n1 * n2 

# divide
def divide (n1, n2):
    return n1 / n2

operations = {"+" : "add",
         "-" : "subtract",
         "*" : "multiply",
         "/" : "divide"
         }


num1 = int(input("What's the first number?"))
for operation in operations:
    print(operation)
sign = input("What do you want to do?")
num2 = int(input("What's the new number?"))

calculation_function = operations[sign]
Answer = calculation_function (num1, num2)
print (f"{num1}{sign}{num2} = {Answer}")
Error:
Answer = calculation_function (num1, num2) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'str' object is not callable [error]
[/error]


RE: Help!!!! Error in Code - Pedroski55 - May-13-2024

Put the function names in only, without any quotes or brackets:

operations = {"+" : add,
             "-" : subtract,
             "*" : multiply,
             "/" : divide
             }
Then

Answer = operations[sign](num1, num2)
There may be other ways to do this!


RE: Help!!!! Error in Code - Ravi1001 - May-13-2024

Thanks!!

worked like a charm!


(May-13-2024, 12:39 PM)Ravi1001 Wrote: Hi,

I am working on a calculator.

I somehow completed it, but i am getting an error.
Unable to figure out.

Help!

Thanks!

# Add
def add (n1, n2):
    return n1 + n2 

# substract
def subtract(n1, n2):
    return n1 - n2 

# multiply
def multiply (n1, n2):
    return n1 * n2 

# divide
def divide (n1, n2):
    return n1 / n2

operations = {"+" : "add",
         "-" : "subtract",
         "*" : "multiply",
         "/" : "divide"
         }


num1 = int(input("What's the first number?"))
for operation in operations:
    print(operation)
sign = input("What do you want to do?")
num2 = int(input("What's the new number?"))

calculation_function = operations[sign]
Answer = calculation_function (num1, num2)
print (f"{num1}{sign}{num2} = {Answer}")
Error:
Answer = calculation_function (num1, num2) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'str' object is not callable [error]
[/error]



RE: Help!!!! Error in Code - ebn852_pan - May-14-2024

Did it work? Well actually you cannot call a string function in Python. You must assign a value to it. x = " " " What do you want to do?" " " # This now becomes a type string data. So rewrite it again.