Ino van den Berg Ino van den Berg - 22 days ago 5
Python Question

My program for getting the average doesn't quite work

def opdracht3()
a = True
result = 0
waslijst = []
while a:
n = input("Enter a number: ")
if n == "stop":
a = False
else:
waslijst += n
for nummer in waslijst:
result += int(nummer)
eind = result / len(waslijst)
print(eind)
opdracht3()


I want to get the average of the list that is being created, but when I add numbers like 11, the len(waslijst) gets set to 2 instead of 1. Is there another way to get the average, or am I using the len function wrong?

Answer

You need use .append method to store all elements in a list.

def opdracht3():
    a = True
    result = 0
    waslijst = []
    while a:
       n = input("Enter a number: ")
       if n == "stop":
          a = False
       else:
          waslijst.append(n)
    for nummer in waslijst:
       result += int(nummer)
    eind = result / len(waslijst)
    print(eind)
opdracht3()