Python 3.x 尝试更正函数

Python 3.x 尝试更正函数,python-3.x,function,Python 3.x,Function,我对用python(或任何东西)编写代码非常陌生。我必须创建一个函数,该函数将根据制动时车辆打滑的距离返回车辆的速度。当我尝试运行doctests时,我得到一个“float”对象不可调用错误。我试过用几种方法更改变量的名称,但似乎都不正确。可能遗漏了一些简单的东西 import math def car_speed(distance_of_skid): ''' Calculate the speed in MPH of a car that skidded d feet on dry

我对用python(或任何东西)编写代码非常陌生。我必须创建一个函数,该函数将根据制动时车辆打滑的距离返回车辆的速度。当我尝试运行doctests时,我得到一个“float”对象不可调用错误。我试过用几种方法更改变量的名称,但似乎都不正确。可能遗漏了一些简单的东西

import math

def car_speed(distance_of_skid):
    '''
Calculate the speed in MPH of a car that skidded
d feet on dry concrete when the brakes were applied

args:
    distance_of_skid (float): the distance of the skid in feet

returns:
    an estimate of the speed the car was going when the brakes were applied (float)

formula:
    speed in MPH equals the square root of (24 * d)

examples/doctest:

the car didn't skid at all
>>> round(car_speed(0), 2)
0.0

the car skid 1 foot
>>> round(car_speed(1), 2)
4.9

the car skid 10 feet
>>> round(car_speed(10), 2)
15.49

the car skid 33.33 feet
>>> round(car_speed(33.33), 2)
28.28

the car skid 12345 feet
>>> round(car_speed(12345), 2)
544.32

'''
    d = distance_of_skid
    car_speed = math.sqrt (24 * d)

    return (round(car_speed(d), 2))

感觉我把事情弄得更复杂了(教授之前提到过这一点)。该函数的输出应该是速度(以英里/小时为单位),四舍五入到小数点后2位。

多亏了藏红花指甲,我才能够计算出来。我使用函数名作为常量,这导致了问题。将常量名称更改为其他名称修复了它。将车辆速度改为返回线中的仅速度

> '''
>     to run tests on mac: python3 -m doctest car_speed.py -v
>     to run tests on Win: python -m doctest car_speed.py -v ''' import math
> 
> def car_speed(distance_of_skid):
>     '''
>     Calculate the speed in MPH of a car that skidded
>     d feet on dry concrete when the brakes were applied
> 
>     args:
>         distance_of_skid (float): the distance of the skid in feet
> 
>     returns:
>         an estimate of the speed the car was going when the brakes were applied (float)
> 
>     formula:
>         speed in MPH equals the square root of (24 * d)
> 
>     examples/doctest:
> 
>     the car didn't skid at all
>     >>> round(car_speed(0), 2)
>     0.0
> 
>     the car skid 1 foot
>     >>> round(car_speed(1), 2)
>     4.9
> 
>     the car skid 10 feet
>     >>> round(car_speed(10), 2)
>     15.49
> 
>     the car skid 33.33 feet
>     >>> round(car_speed(33.33), 2)
>     28.28
> 
>     the car skid 12345 feet
>     >>> round(car_speed(12345), 2)
>     544.32
> 
>     '''
>     # TO DO: Add your code here
>     d = distance_of_skid
>     speed = math.sqrt (24 * d)
> 
>     return (round(speed, 2))

你期望车速(d)返回多少?它需要返回车速(以英里/小时为单位),四舍五入到2位。啊,我浏览了文件的开头。我的错。DX那么看起来你是想从函数内部调用函数?在function.yep的最后一行中,这就是错误。只要我更改了常量名称并重新运行,它就工作了。我知道这很简单。谢谢很高兴听到你把它修好了!您是否介意创建一个解释您所做更改的答案,包括显示修改后的代码,以方便将来的读者?