【问题标题】:How to add a float number into a list until the lenght of the list equals N number?如何将浮点数添加到列表中,直到列表的长度等于 N 数?
【发布时间】:2019-09-22 20:50:17
【问题描述】:

我必须创建一个程序,它询问浮点数列表的长度,然后它应该询问列表中的浮点数,最后它会打印这些数字的平均值。使用 Python 3。

我已尝试使用 list.extend 并将带有分配的数字添加到列表中并添加浮点输入。

numbersInTheList=int(input())
thoseNumbers=[]
while len(thoseNumbers)!=numbersInTheList:
    thoseNumbers=thoseNumbers + float(input())    
print(sum(thoseNumbers)/numbersInTheList)

我希望输出是列表中数字的平均值。

【问题讨论】:

    标签: python-3.x list add items


    【解决方案1】:

    list.extend 用于将一个列表扩展到另一个列表。在这种情况下,您希望将append 单个值添加到现有列表中。

    numbersInTheList= int(input())
    thoseNumbers = []
    while len(thoseNumbers) != numbersInTheList:
        thoseNumbers.append(float(input()))
    print(sum(thoseNumbers)/numbersInTheList)
    

    【讨论】:

      最近更新 更多