howManyNames = (float(input("Enter how many student names do you want to enter? ")))
studentNames = []
ages = []
averageAge = 0
counter = 0
while (counter < int(howManyNames)):
studentNames.append(input("Enter student names. "))
ages.append(float(input("Enter that persons age. ")))
counter += 1
averageAge = (float(ages)) / (float(howManyNames))
print (averageAge)
我一直得到这样的类型错误:float()参数必须是字符串或数字
我知道了,但我似乎找不到我的错误,我知道你不能用和浮除数组。。。。谢谢大家!你知道吗
更改:
收件人:
(注意:出于美观的考虑,我刚刚删除了多余的括号。)
解释:
如果打开repl并键入
您将获得
float
的文档,其中说明:换句话说,您可以:
但你不能这样做:
因为
[]
是一个list
,而不是一个字符串或数字,根据它的文档,float
是可以接受的。float
接受列表也没有意义,因为它的目的是将字符串或数字转换为浮点值。你知道吗在你的问题中,你定义了:
正在将
ages
设置为[]
(类型为list
)当然,要找到平均值,需要取这些值的和除以它们的值。当然,
python
碰巧有一个内置的sum
函数,它将为您汇总一个列表:你只需要除以数值的个数就可以转换平均值。你知道吗
相关问题 更多 >
编程相关推荐