为什么我得到一个类型错误?

2024-06-17 11:51:13 发布

您现在位置:Python中文网/ 问答频道 /正文

import time

def average(numbers):
    "Return the average (arithmetic mean) of a sequence of numbers."
    return sum(numbers) / float(len(numbers)) 

#example function

def domath(a,b,c):
    a+b+c
    a*b*c
    a/b/c
    a-b-c
    a^b
    b^c


def timedcalls(n, fn, *args):
    times=[]
    if type(n)==int:
        t0 = time.clock()
        for rep in range(n):
            t0 = time.clock()
            fn(*args)
            t1 = time.clock()
            times.append(t1-t0)
    else:
        start=time.clock()
        while time.clock-start<int(n):
            t0 = time.clock()
            fn(*args)
            t1 = time.clock()
            times.append(t1-t0)    
    return min(times), average(times), max(times)

print timedcalls(5.0, domath, 1,2,3)

这段代码可以很好地处理int类型,但是由于某种原因,如果我使用float,它会给我这个错误。在

^{pr2}$

这条线:

return min(times), average(times), max(times)

Tags: ofreturntimedefargsfloatintfn
1条回答
网友
1楼 · 发布于 2024-06-17 11:51:13
while time.clock-start<int(n):

应该是

^{pr2}$

您没有调用time.clock函数

注意在回溯中,它说它不能在'builtin_function_or_method' and 'float'之间做减法

相关问题 更多 >