Reputation: 1
Ok, so I made a basic calculator today, (and today is my first day of learning programming) and I'm getting this error with the following code:
Traceback (most recent call last):
File "C:\Users\Stone\Desktop\unfinished calculator.py", line 42, in <module>
start()
File "C:\Users\Stone\Desktop\unfinished calculator.py", line 26, in start
n1()
TypeError: 'int' object is not callable
with this as the code
x = 1
global n1
global n2
global opp1
def n1():
global n1
n1 = input("Number to perform operation on: ")
n1 = int(n1)
def n2():
global n2
n2 = input("Number to perform operation with: ")
n2 = int(n2)
def opp():
global opp1
opp1 = input("Available operations to perform with number:\n1)Addition\n2)Subtraction\n3)Multiplication\n4)Division\n5)Exit Calculator\nPlease enter number choice (1-5): ")
opp1 = int(opp1)
def add():
print(n1 + n2)
def subtract():
print (n1 - n2)
def multiply():
print(n1 * n2)
def divide():
print(n1 / n2)
def start():
n1()
opp()
n2()
if opp1 == 1:
add()
elif opp1 == 2:
subtract()
elif opp1 == 3:
multiply()
elif opp1 == 4:
divide()
elif opp1 == 5:
x = 0
else:
print("Invalid Choice!")
while x == 1:
start()
Can someone please explain to me what's wrong here?
Upvotes: 0
Views: 1297
Reputation: 925
The problem is you are defining n1
as both a function and a variable. It can't be both. I would recommend changing the name of your def n1():
function.
To expand a bit more, on line 2, you have this:
global n1
But on line 5, you have this:
def n1():
The first was setting a global variable that can be accessed from any function in your file. The second is creating a specific function. To put it simply, they can't both have the same name in the the same scope like this. So on line 26 you call n1
, which is actually a variable and not a function, and the Python interpreter errors out because you can't 'call' an int like you can call a method.
A quick fix would be to rename your variables n1
and n2
to be something other than your method names n1
and n2
. However, as you keep learning to program, you'll learn how to pass variables into your method as well as return them when the method is finished. This means you won't even have to use global variables (which are often considered a bad thing).
So rather than declaring global n1
, you could delete that line, and define your n1
function to be this:
def n1():
number = input('Number to perform operation on: ')
try:
return int(number)
except ValueError:
print("That's not a number.")
To break down what's going on there:
The try
statement attempts to execute a block of code. In this case, it tries to convert what the user entered into an integer. If it's not a number, an exception
occurs, and we print out "That's not a number", and return back to your start
function. (Alternatively, you may want to put it in a while loop, so it will keep asking until the user enters a number. This question may help.) If it is a number, the return
statement occurs. This returns the result of that function back to where you called it. So back in your start()
function, you would do something like this:
value1 = n1()
That assigns the result of n1
to your new variable value1
which you can then use.
Upvotes: 1