-3

Need a code to calculate the mean of a list of numbers entered by users that stops when “stop” is entered, but am unsure how to use 'while' to continue the loop and 'if' to end.

number = int(input("Enter an integer"))
n=1
while number>0:
    number+=int(input("Enter an integer"))
    n+=1
    print(number/n)

1 Answers1

0

You need to keep the number you just entered separate from your running sum.

number = int(input("Enter an integer"))
n = 1
total = number
while number > 0:
    number = int(input("Enter an integer"))
    n += 1
    total += number
    print(total/n)

This is simpler if you use an infinite loop with an explicit break statement, so that you don't need to repeat the call to input in two places.

total = 0

while True:
    number = int(input("Enter an integer"))
    if number <= 0:
        break
    n += 1
    total += number
    print(number/n)
chepner
  • 497,756
  • 71
  • 530
  • 681