我正在编写涉及用户定义函数的代码,这是我的新知识。
from statistics import *
years = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016]
woodDeaths = [19.6, 10.4 ,10.5 ,13.8, 23.1, 19.6, 26.7, 27.8, 53.7]
mcdowellDeaths = [102.2, 103.3, 108.5, 119.7, 70.3, 105.4, 107.6, 141.2, 73.1]
mercerDeaths = [46.9, 22.5, 54.6, 81.6, 59.2, 40.3, 76.1, 62.1, 74.4]
raleighDeaths = [24.2, 15.2, 57.1, 75.8, 73.4, 46.9, 49.8, 80, 58.7]
def get_average(raw_data):
raw_data = sum(raw_data) / len(raw_data)
return raw_data
def calculate_z_scores(raw_data):
z_scores = []
for i in raw_data:
z_score = i - (sum(raw_data) / len(raw_data)) / raw_data.stdev
append.z_scores(z_score)
return z_scores
def print_stats(deaths_data, county):
get_average(deaths_data)
print (f" In {county} the average deaths was {deaths_data}")
calculate_z_scores(deaths_data)
z_scores = calculate_z_scores
for i in years:
print (f" In {years}, the z-score is: {z_score}")
def main():
print_stats (woodDeaths, "Wood County")
print_stats (mcdowellDeaths, "McDowell County")
print_stats (mercerDeaths, "Mercer County")
print_stats (raleighDeaths, "Raleigh County")
if __name__ == "__main__":
main()
但是,每当我尝试运行代码时,我都会遇到一个错误,修复它,然后又出现另一个错误。谁能帮我解决这个问题? The Example Output
In Wood County the average deaths was [19.6, 10.4, 10.5, 13.8, 23.1, 19.6, 26.7, 27.8, 53.7]
Traceback (most recent call last):
File "C:\Users\brenn\Documents\python\bouillion_brennan_hw3_ocp.py", line 33, in <module>
main()
File "C:\Users\brenn\Documents\python\bouillion_brennan_hw3_ocp.py", line 28, in main
print_stats (woodDeaths, "Wood County")
File "C:\Users\brenn\Documents\python\bouillion_brennan_hw3_ocp.py", line 23, in print_stats
calculate_z_scores(deaths_data)
File "C:\Users\brenn\Documents\python\bouillion_brennan_hw3_ocp.py", line 17, in calculate_z_scores
z_score = i - (sum(raw_data) / len(raw_data)) / raw_data.stdev
AttributeError: 'list' object has no attribute 'stdev'
这是我现在得到的错误
答案 0 :(得分:0)
有很多错误,但这是一个清理的版本。
from statistics import *
years = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016]
woodDeaths = [19.6, 10.4 ,10.5 ,13.8, 23.1, 19.6, 26.7, 27.8, 53.7]
mcdowellDeaths = [102.2, 103.3, 108.5, 119.7, 70.3, 105.4, 107.6, 141.2, 73.1]
mercerDeaths = [46.9, 22.5, 54.6, 81.6, 59.2, 40.3, 76.1, 62.1, 74.4]
raleighDeaths = [24.2, 15.2, 57.1, 75.8, 73.4, 46.9, 49.8, 80, 58.7]
def get_average(raw_data):
raw_data = sum(raw_data) / len(raw_data)
return raw_data
def calculate_z_scores(raw_data):
z_scores = []
for i in raw_data:
z_score = (i - (sum(raw_data) / len(raw_data))) / stdev(raw_data)
z_scores.append(z_score)
return z_scores
def print_stats(deaths_data, county):
get_average(deaths_data)
print (f" In {county} the average deaths was {get_average(deaths_data):0.0f}")
calculate_z_scores(deaths_data)
z_score = calculate_z_scores(deaths_data)
for year,z_score in zip(years,z_score):
print (f" In {year}, the z-score is: {z_score:0.4f}")
def main():
print_stats (woodDeaths, "Wood County")
print_stats (mcdowellDeaths, "McDowell County")
print_stats (mercerDeaths, "Mercer County")
print_stats (raleighDeaths, "Raleigh County")
if __name__ == "__main__":
main()
您的最初错误:AttributeError: 'list' object has no attribute 'stdev'
是因为您试图调用列表的属性stdev,但是该属性不存在。但是,由于导入了统计信息模块,因此可以在列表上调用方法stdev
。
就像我说的那样,代码中有很多错误。我可以列举一下,但要花点时间。如果您有任何疑问,请告诉我。
作为实现大型代码块的建议:在将每个函数或代码块实现到程序中之前,请确保它们能正常工作。
输出:
In Wood County the average deaths was 23
In 2008, the z-score is: -0.2418
In 2009, the z-score is: -0.9368
In 2010, the z-score is: -0.9292
In 2011, the z-score is: -0.6799
In 2012, the z-score is: 0.0227
In 2013, the z-score is: -0.2418
In 2014, the z-score is: 0.2946
In 2015, the z-score is: 0.3777
In 2016, the z-score is: 2.3345
In McDowell County the average deaths was 103
In 2008, the z-score is: -0.0590
In 2009, the z-score is: -0.0082
In 2010, the z-score is: 0.2317
In 2011, the z-score is: 0.7485
In 2012, the z-score is: -1.5309
In 2013, the z-score is: 0.0887
In 2014, the z-score is: 0.1902
In 2015, the z-score is: 1.7406
In 2016, the z-score is: -1.4017
In Mercer County the average deaths was 58
In 2008, the z-score is: -0.5600
In 2009, the z-score is: -1.8465
In 2010, the z-score is: -0.1541
In 2011, the z-score is: 1.2694
In 2012, the z-score is: 0.0885
In 2013, the z-score is: -0.9080
In 2014, the z-score is: 0.9795
In 2015, the z-score is: 0.2414
In 2016, the z-score is: 0.8898
In Raleigh County the average deaths was 53
In 2008, the z-score is: -1.3054
In 2009, the z-score is: -1.7070
In 2010, the z-score is: 0.1626
In 2011, the z-score is: 0.9970
In 2012, the z-score is: 0.8899
In 2013, the z-score is: -0.2925
In 2014, the z-score is: -0.1631
In 2015, the z-score is: 1.1844
In 2016, the z-score is: 0.2340