【问题标题】:TypeError: unsupported operand type(s) for *: 'int' and 'function' Not seeing why and which one is a function类型错误:* 的不支持的操作数类型:'int' 和 'function' 看不到为什么以及哪个是函数
【发布时间】:2015-11-17 01:29:54
【问题描述】:

我不知道为什么我在将一个 int 和一个函数相乘时遇到错误。

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'

代码:

#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()

【问题讨论】:

  • get_side_length 正在读取并丢弃一个值,然后返回自身。我认为您需要了解变量和函数的实际工作方式;这段代码充满了错误,这些错误暴露了对基本编程概念的根本误解。
  • 我是一名编程专业的学生,​​他的第一季度已经完成了一半。我正在学习这些基本概念并且刚刚开始使用功能。

标签: python function int


【解决方案1】:

您调用函数的方式会导致问题。

side_length = get_side_length

上面的代码将side_length分配给函数本身。要将side_length 指定为函数返回的值,请使用:

side_length = get_side_length()

同样,

area = polygon_area(num_sides, side_length)

而get_side_length函数应该是:

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

由于side_length 在您的代码中引用了一个函数,因此您会收到上述错误。

【讨论】:

    【解决方案2】:

    很抱歉没有仔细阅读您的代码。你可能需要知道一个函数应该返回一些东西。例如,在您的get_side_length 中,应该返回整数的结果。

    我已经更改了您的代码,现在应该可以使用了。

    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
    

    【讨论】:

    • area = (num_sides * side_length * side_length) / \ TypeError: unsupported operand type(s) for *: 'int' and 'function' >>>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2013-10-04
    • 2021-06-16
    • 2014-06-16
    • 2014-03-16
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多