Search code examples
python-3.xlistadditionitems

How to add a float number into a list until the lenght of the list equals N number?


I have to create a program where it asks for the length of a list of float numbers, then it should ask for the float numbers in the list and finally it will print the average of those numbers. Working with Python 3.

I have tried with list.extend and adding the number with an assignment into the list and adding a float input.

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

I expect the output to be the average of the numbers in the list.


Solution

  • list.extend is used to extend one list with another. In this case, you want to append a single value to an existing list.

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