Wyntre
Wyntre

Reputation: 1

TypeError: unsupported operand type(s) for *: 'int' and 'function' Not seeing why and which one is a function

I can't figure out why I am getting an error with having an int and a function multiplying.

File "E:/Fundamentals of Programming/Programs/polygon_area.py", line 23, in polygon_area
    area = (num_sides * side_length * side_length) / \
TypeError: unsupported operand type(s) for *: 'int' and 'function'

Code:

#this program computes
#the area of polygons

import math


def main():
    get_side_length()
    side_length = get_side_length
    report(side_length)

    def report(side_length):
        print('side length \t number of sides \t area')

    for i in range(3, 10):
        num_sides = i
        polygon_area(num_sides, side_length)
        area = polygon_area
        print(side_length, '\t', num_sides, '\t', area)


def polygon_area(num_sides, side_length):
    area = (num_sides * side_length * side_length) / \
           (4 * math.tan(math.pi / num_sides))
    return area


def get_side_length():
    int(input('Input the length of a side. '))
    return get_side_length


#start program
main()

Upvotes: 0

Views: 3718

Answers (2)

sunhs
sunhs

Reputation: 401

I'm sorry for not having read your code carefully. You may need to know that a function should return something. And in your get_side_length, for example, the result, which is an integer, should be returned.

I've changed your code, which should work now.

import math

def main():
    side_length = get_side_length() # get_side_length returns an integer and assigns it to side_length
    report(side_length)

    def report(side_length):
        print('side length \t number of sides \t area')

    for i in range(3, 10):
        num_sides = i
        area = polygon_area(num_sides, side_length) # polygon_area returns an number representing the area and assigns it to area
        print(side_length, '\t', num_sides, '\t', area)


def polygon_area(num_sides, side_length):
    area = (num_sides * side_length * side_length) / \
           (4 * math.tan(math.pi / num_sides))
    return area


def get_side_length():
    return (input('Input the length of a side. ')) # you get an integer from input and should return it

Upvotes: 0

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

The way you call functions causes the issue.

side_length = get_side_length

The above code assigns side_length with the function itself. To assign side_length as the value returned by the function, use:

side_length = get_side_length()

Similiarly,

area = polygon_area(num_sides, side_length)

And get_side_length function should be:

def get_side_length():
    side_length = int(input('Input the length of a side. '))
    return side_length

Since side_length refers to a function in your code, you get the above error.

Upvotes: 1

Related Questions