def avg_value(array): sum = 0 for member in array: sum = sum + member avg = sum/len(array) avg_toStr = str(avg) retuen avg_toStr[0:4] f_s_y=[1,2,3,4,5] m_s_y=[23,23,24,2,4] print('hhh' + str(avg_value(f_s_y)))
时间: 2024-02-04 16:02:43 浏览: 111
你的代码中存在一个错误,函数中的return语句拼写错误,应该是return而不是retuen。此外,字符串切片的语法也有问题,应该是avg_toStr[0:4],而不是avg_toStr[0:4]。修改后的代码如下:
```
def avg_value(array):
sum = 0
for member in array:
sum = sum + member
avg = sum/len(array)
avg_toStr = str(avg)
return avg_toStr[0:4]
f_s_y=[1,2,3,4,5]
m_s_y=[23,23,24,2,4]
print('hhh' + str(avg_value(f_s_y)))
```
这样的话,输出结果应该是:"hhh3.0"。
相关问题
def avg_value(array): sum = 0 for member in array: sum = sum + member avg = sum/len(array) avg_toStr = str(avg)[0:4] f_s_y=[1,2,3,4,5] m_s_y=[23,23,24,2,4] print('hhh' + str(avg_value(f_s_y))
你的代码中存在一个错误,函数没有正确结束。我猜测你的问题是关于代码的输出结果。根据你的代码,输出应该是:"hhh3.0",因为f_s_y列表的平均值是3.0。但是这个输出并不会直接出现在控制台上,因为代码中的print语句没有正确结束。你需要在print语句后面加上一个闭合的括号")"。修改后的代码如下:
```
def avg_value(array):
sum = 0
for member in array:
sum = sum + member
avg = sum/len(array)
avg_toStr = str(avg)[0:4]
return avg_toStr
f_s_y=[1,2,3,4,5]
m_s_y=[23,23,24,2,4]
print('hhh' + str(avg_value(f_s_y)))
```
这样的话,输出结果应该是:"hhh3.0"。
请将下述程序补充完整,计算并输出Peter各科平均分。 提示:使用**参数以字典形式吸收关键字参数。 程序期望的执行结果为: math : 90 english : 88 python : 95 sports : 76 Peter's average score is:87.25 def key(sName= Peter ,____1_____): sum=0 for i,j in course.items(): print(i, : ,j) for i in ____2_____: sum=sum+i avg=sum/_____3___ print( {}'s average score is:{:.2f} .format(sName,avg)) key(math=90,english=88,python=95,sports=76)
```
def key(sName="Peter", **course):
sum = 0
for i, j in course.items():
print(i, ":", j)
for i in course.values():
sum += i
avg = sum / len(course)
print("{}'s average score is:{:.2f}".format(sName, avg))
key(math=90, english=88, python=95, sports=76)
```
阅读全文