浮点除零

时间:2015-01-06 19:53:05

标签: python

我需要进行一些简单的数学计算,但它会返回“零除以”的分数。错误。这是我的代码:

sec = getSec(str(form.cleaned_data['length'])) #seconds from TimeField
dist = form.cleaned_data['distance'] #distance, FloatField
spd = getSpeed(dist, sec) #average speed, should be FLOAT

以及我的功能getSpeed

def getSpeed(distance, length):
    return distance/(length/3600)

如果我保持这种方式,如果我将功能更改为

,我会获得ZeroDivisionError at /addtraining float division by zero
def getSpeed(distance, length):
    return distance/(float(length)/3600)

我得到了TypeError at /addtraining float() argument must be a string or a number

编辑:getSec功能:

def getSec(s):
l = s.split(':')
h = int(l[0]) * 3600
m = int(l[1]) * 60
s = int(l[2])
if h == 0:
    return m + s
else:
    return h + m + s

任何人都可以帮助我吗?任何帮助将不胜感激。

0 个答案:

没有答案