0

I'm trying to import a txt file into python that contains a list of numbers. I'm suppose to get the average of the numbers and the standard deviation of the numbers in the file. My script is wrong though, can someone show me the right way.

from sys import argv
script, filename = argv
txt = open(filename)
for line in txt:
    total = 0
    length = 0
    amount = float(line)
    total += amount
    length = length + 1
average = total/length
print average
4

2 回答 2

8
total = 0
length = 0

应该在循环之外。

于 2012-10-01T13:16:14.460 回答
2

将以下语句移到循环之外:-

total = 0
length = 0
于 2012-10-01T13:16:56.207 回答