标准偏差错误Python:除以零

时间:2015-03-12 19:07:40

标签: python math

所以,我是第一次编程课程,我们正在学习和使用Python。我在实验室遇到问题,我收到此错误消息:

Traceback (most recent call last):
  File "/Users/kellyslane/Desktop/lab04.py", line 106, in <module>
    main()
  File "/Users/kellyslane/Desktop/lab04.py", line 35, in main
    sd2 = standard(dev1, sd1, std)
  File "/Users/kellyslane/Desktop/lab04.py", line 93, in standard
    b[k] = float(a[k]/c)
ZeroDivisionError: float division by zero

我的错误似乎是基于数学的,所以我不会认为它是我的代码的问题,但也许我的公式?这是代码:

# Program to calculate standard deviation and standard score
n = 20

import math

def main():

    # file declaration
    infile = open('lab04.dat', 'r')
    outfile = open('lab04.out', 'w')
    # list/array declaration
    score = [0.0] * 20
    dev = [0.0] * 20
    dev1 = [0.0] * 20
    sd1 = [0.0] * 20
    # variable declaration
    sumx = 0.0
    xbar = 0.0
    dev2 = 0.0
    std = 0.0
    sd2 = 0.0
    # call function
    sumx = loadfun(infile, score)
    # compute average
    xbar = float(sumx/n)
    # call function
    deviation2 = deviation(score, dev, dev1, xbar)
    # compute standard deviation
    std = float(math.sqrt(dev2/n))
    print(std)
    # call function
    sd2 = standard(dev1, sd1, std)
    # call function
    outdata(outfile, score, dev, dev1, sd1)
    # print headers
    print('Statistical Analysis')
    outfile.write('Statistical Analysis')
    # print rest of output
    print('Score = ', score)
    outfile.write('Score = ' + str(score))
    print('Sum = ', sumx)
    outfile.write('Sum = ' + str(sumx))
    print('Average = ', xbar)
    outfile.write('Average = ' + str(xbar))
    print('Deviation = ', dev)
    outfile.write('Deviation = ' + str(dev))
    print('Deviation1 = ', dev1)
    outfile.write('Deviation1 = ' + str(dev1))
    print('Deviation2 = ', dev2)
    outfile.write('Deviation2 = ' + str(dev2))
    print('Standard Deviation = ', std)
    outfile.write('Standard Deviation = ' + str(dev2))
    print('Standard Scores = ', sd1)
    outfile.write('Standard Scores = ' + str(sd1))
    print('Sum of Standard Scores = ', sd2)
    outfile.write('Sum of Standard Scores = ' + str(sd2))
    infile.close
    outfile.close




def loadfun(file, a):

    # local variable
    k = 0
    s = 0
    while (k < n):
        templist = file.readline().strip('\n').split('\n')
        a[k] = float(templist[0])
        s = s + a[k]
        k = k + 1
    return (s)

def deviation(a, b, c, d):

    # local variable
    k = 0
    s = 0
    while (k < n):
        b[k] = float(d - a[k])
        c[k] = float(b[k] * b[k])
        s = s + c[k]
        k = k + 1
    return (s)

def standard(a, b, c):

    # local variable
    k = 0
    s = 0
    while (k < n):
        b[k] = float(a[k]/c)
        s = s + b[k]
        k = k + 1
    return(s)

def outdata(file, a, b, c, d):

    # local variable
    k = 0
    while (k < n):
        print(a[k], b[k], c[k], d[k])
        file.write(str(a[k]) + str(b[k]) + str(c[k]) + str(d[k]))
        k = k + 1

main()

我的数据文件包含以下数据:

72    
90    
80    
85    
76    
70    
68    
84    
92    
80    
50    
60    
73    
89    
100    
40    
75    
76    
94    
86

2 个答案:

答案 0 :(得分:1)

你除以零:

dev2 = 0.0 #zero
...
std = float(math.sqrt(dev2/n)) #0 divided by non-zero int equals 0
...
sd2 = standard(dev1, sd1, std) #passing in 0 for c

答案 1 :(得分:1)

您计算的是deviation2,但您使用的是dev2。所以你应该做到以下几点:

std = float(math.sqrt(deviation2 / n))

而不是

std = float(math.sqrt(dev2 / n))

或只是:

dev2 = deviation(score, dev, dev1, xbar)