Home > Enterprise >  I keep getting TypeError: 'int' object is not callable
I keep getting TypeError: 'int' object is not callable

Time:11-11

I'm trying to convert temperature to either Celcius or Fahrenheit and below is my code:

temp = eval(input('Enter a temperature: '))
unit = input('C(Cel) or F(Faren): ')
if unit.upper() == 'C':
  celcius = 5/9(temp-32)
  print(celcius)
else:
  fahrenheit = 9/5(temp 32)
  print(fahrenheit)

But I keep getting this output:

TypeError: 'int' object is not callable

Please how do I solve this problem?

CodePudding user response:

5/9(temp-32)

I assume you wanted to multiply here, the right thing to do it
5/9 * (temp-32), same with 9/5 * (temp 32)

Also, replace temp = eval(input('Enter a temperature: '))
with temp = int(input('Enter a temperature: '))

eval is super dangerous since it can let the user of the program run anything on your computer, you should almost always avoid it

CodePudding user response:

Both celcius = 5/9(temp-32) and fahrenheit = 9/5(temp 32) are mistyped. Python now thinks it's a function. U need to add the multiplication charcter * like this:

temp = int(input('Enter a temperature: '))
unit = input('C(Cel) or F(Faren): ')
if unit.upper() == 'C':
  celcius = 5/9*(temp-32)
  print(celcius)
else:
  fahrenheit = 9/5*(temp 32)
  print(fahrenheit)

Notice that I also changed your eval(...) to int(...) this converts the string output of input(...)to an integer, which you need to perform mathematical actions. Added benefit is that the removal of eval() makes your code a whole lot safer.

  • Related