尽管代码先前已按原样运行,但预期的缩进错误

时间:2017-10-06 02:34:30

标签: python

  

更正代码

'''Find the variance and standard deviation of a list of numbers'''

def calculate_mean(numbers):
    s = sum(numbers)
    N = len(numbers)
# Calculate the mean
    mean = s/N

    return mean

def find_differences(numbers):
# Find the mean
    mean = calculate_mean(numbers)
# Find the differences from the mean
    diff = []
    for num in numbers:
            diff.append(num-mean)

    return diff

def calculate_variance(numbers):

# Find the list of differences
    diff = find_differences(numbers)
# Find the squared differences
    squared_diff = []
    for d in diff:
            squared_diff.append(d**2)
# Find the variance
    sum_squared_diff = sum(squared_diff)
    variance = sum_squared_diff/len(numbers)
    return variance

if __name__ == '__main__':
    donations = [100, 60, 70, 900, 100, 200, 500, 500, 503, 600, 1000, 1200]
    variance = calculate_variance(donations)
print('The variance of the list of numbers is {0}'.format(variance))
std = variance**0.5
print('The standard deviation of the list of numbers is {0}'.format(std))

我在这里做错了什么,因为此代码以前有效,但今天要求我缩进。对于noob问题我很抱歉,但我不熟悉编码。预期的缩进块错误发生在第4行代码中。

'''Find the variance and standard deviation of a list of numbers'''

def calculate_mean(numbers):
s = sum(numbers)
N = len(numbers)
# Calculate the mean
mean = s/N

return mean

def find_differences(numbers):
# Find the mean
mean = calculate_mean(numbers)
# Find the differences from the mean
diff = []
for num in numbers:
diff.append(num-mean)

return diff

def calculate_variance(numbers):

# Find the list of differences
diff = find_differences(numbers)
# Find the squared differences
squared_diff = []
for d in diff:
squared_diff.append(d**2)
# Find the variance
sum_squared_diff = sum(squared_diff)
variance = sum_squared_diff/len(numbers)
return variance

if __name__ == '__main__':
donations = [100, 60, 70, 900, 100, 200, 500, 500, 503, 600, 1000, 1200]
variance = calculate_variance(donations)
print('The variance of the list of numbers is {0}'.format(variance))
std = variance**0.5
print('The standard deviation of the list of numbers is {0}'.format(std))

下面是代码的缩进版本,但现在我在第38行收到意外的缩进错误

'''Find the variance and standard deviation of a list of numbers'''

def calculate_mean(numbers):
    s = sum(numbers)
    N = len(numbers)
# Calculate the mean
    mean = s/N

return mean

def find_differences(numbers):
# Find the mean
    mean = calculate_mean(numbers)
# Find the differences from the mean
    diff = []
for num in numbers:
    diff.append(num-mean)

return diff

def calculate_variance(numbers):

# Find the list of differences
    diff = find_differences(numbers)
# Find the squared differences
    squared_diff = []
for d in diff:
    squared_diff.append(d**2)
# Find the variance
    sum_squared_diff = sum(squared_diff)
    variance = sum_squared_diff/len(numbers)
return variance

if __name__ == '__main__':
    donations = [100, 60, 70, 900, 100, 200, 500, 500, 503, 600, 1000, 1200]
    variance = calculate_variance(donations)
print('The variance of the list of numbers is {0}'.format(variance))
    std = variance**0.5
print('The standard deviation of the list of numbers is {0}'.format(std))

0 个答案:

没有答案