1

I have this code:

import math

length_centre = float(input("Enter length from centre to corner of pentagon: "))
side_length = 2*length_centre*(math.sin(math.pi/5))
print(side_length)

areaP = (5((side_length)**2))/(4*((math.tan)((math.pi)/5)))

I get an error on the last line which says TypeError: 'int' object is not callable. Why? How do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
DinoMeme
  • 47
  • 1
  • 1
  • 9

1 Answers1

3

Programming languages don't have implicit multiplication like written math, so 5((side_length)**2) is not legal, it's trying to call 5 as a function with argument side_length ** 2. I'm guessing you want 5 * side_length**2 (removing some extraneous parens that aren't needed since exponentiation binds more tightly than other math ops anyway).

Cleaning it all up, you'd have:

import math

# Use float to get the specific type you want, rather than executing whatever
# code the user types. If this is Py2, change input to raw_input too,
# because Py2's input is equivalent to wrapping raw_input in eval already
length_centre=float(input("Enter length from centre to corner of pentagon: "))

# Strip a whole bunch of unnecessary parens
side_length = 2 * length_centre * math.sin(math.pi / 5)
print(side_length)


# Strip a whole bunch of unnecessary parens (left in some technically unnecessary parens
# because they group the complex operands for the division visually)
areaP = (5 * side_length**2) / (4 * math.tan(math.pi / 5))
print(areaP)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • More accurate to say that *Python* doesn't have implicit multiplication. It might be true that very few, if any, languages support it, but it's certainly possible. – chepner Sep 30 '16 at 23:10
  • @chepner: True. I'm sure some domain specific languages for mathematicians might offer this feature, but general purpose programming languages, it's basically unheard of, at least to my knowledge. Though I'm now imagining some evil individual monkey-patching Ruby's `Integer` class to make it callable so using it with parens like this would be equivalent to multiplying by the contained expression... :-) – ShadowRanger Sep 30 '16 at 23:24