Home > Net >  Why does my program to calculate the average of numbers entered into a while loop not work as expect
Why does my program to calculate the average of numbers entered into a while loop not work as expect

Time:11-03

I am new to coding. I would like to attend a course but there are assessments first in order to be chosen.

The question I am stuck on is:

Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.

num = 0
inputs = []

while True:
  num = int(input("Please enter a number between -1 and 5: "))
  if num > -1:
    break
  else:
    inputs.append(int(num))

print (sum(inputs) / len(inputs))

CodePudding user response:

Maybe this is the code you are looking for just changing if num > -1 to if num == -1 to agree with the question you posted:

num = 0
inputs = []

while True:
  num = int(input("Please enter a number between -1 and 5: "))
  if num == -1:
    break
  else:
    inputs.append(num)

print(sum(inputs) / len(inputs))

and it works just fine having as a result:

Please enter a number between -1 and 5: 5
Please enter a number between -1 and 5: 4
Please enter a number between -1 and 5: 3
Please enter a number between -1 and 5: 2
Please enter a number between -1 and 5: 1
Please enter a number between -1 and 5: -1
3.0

CodePudding user response:

so lets break this down together.

This is fine.

num = 0
inputs = []

You will want to modify this while true statement to be while True (if using python) like below

while True:

Next, you want to be sure the value entered is an integer, passing a letter will cause a ValueError. We make a decision to ignore bad input and continue.

  input_value = input("Please enter a number between -1 and 5: ")
  try:
    num = int(input_value)
  except ValueError:
    continue

The condition for stopping the loop is entering -1 (if I understood the question properly). So, first check to see if the number entered matches the condition to stop: if num == -1: break.

if num == -1:
    break

otherwise, you will want to check to be sure the number is in range before it is added to your inputs array, otherwise ignore it and wait for the next input:

elif num < -1 or num > 5:
    continue

Finally, if input is not -1, and the input is a number between 0-5 inclusive, add it to the array and await the next input.

  else:
    inputs.append(num)

Finally at the end of the return after -1 was received, it is possible the first number entered was -1, or all values entered were invalid. Both of these scenarios would result in division by 0, and an error being thrown. So you would want to set the minimum number that can be set as the divisor to 1 to ensure you don't crash trying to calculate your answer:

print(sum(inputs) / max(len(inputs), 1))

And now putting it all together:

num = 0
inputs = []

while True:
  input_value = input("Please enter a number between -1 and 5: ")
  try:
    num = int(input_value)
  except ValueError:
    continue
  if num == -1:
    break
  elif num < -1 or num > 5:
    continue
  else:
    inputs.append(num)

return sum(inputs) / max(len(inputs), 1)
  • Related