首页 > 解决方案 > TypeError: unsupported operand type(s) for *: 'function' and 'int' in Python, 无法计算解决方案

问题描述

我尝试从其他示例中理解此错误,但我无法弄清楚。代码在标题中返回错误。

def truckloads_of_asphalt(miles, lanes, asphalt_depth):
    total_feet = ((miles*5280) * (lanes * 12)) * (asphalt_depth/12)
    total_asphalt_truck = math.ceil((((total_feet * 145)/2000))/5)
    return total_asphalt_truck

asphalt_cost = (truckloads_of_asphalt * 5 * 145)
print('Cost of asphalt : ', (asphalt_cost))

问题基本上是我试图打印沥青成本的值,这是结果:(truckloads_of_asphalt * 5 * 145)但我收到了这个错误。

标签: python

解决方案


除了你没事之外,你 从来没有打电话给你function并将返回值分配给它variable

import math

def truckloads_of_asphalt(miles, lanes, asphalt_depth):
    total_feet = ((miles*5280) * (lanes * 12)) * (asphalt_depth/12)
    total_asphalt_truck = math.ceil((((total_feet * 145)/2000))/5)
    return total_asphalt_truck

vash  = truckloads_of_asphalt(60, 2, 4)
asphalt_cost = (vash * 5 * 145)
print('Cost of asphalt : ', (asphalt_cost))
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 trucks.py
Cost of asphalt :  26643025

值得熟悉f-strings

print(f"Cost of asphalt: {asphalt_cost}")

推荐阅读