I have just started coding and am stuck solving this python problem

Question:

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.

This is what I have come up with so far

num = int(input("How many number's are there?"))
total_sum = 0

avg = total_sum / num 

for n in range (num):
  number = float(input("Enter a number"))
while number <0:
  
  print = ("Average is", avg)
  
 
number >0
print(number)
  
total_sum += number
  
avg = total_sum / num 
print = ("Average is", avg)

I need it to stop at -1 one and still give an average of the numbers listed.

Asked By: Seg

||

Answers:

Easy.

emp = []

while True:
    ques = int(input("Enter a number: "))
    if ques != -1:
        emp.append(ques)
        continue # continue makes the code go back to beginning of the while loop
    else:
        if len(emp) != 0:
            avg = sum(emp) / len(emp) # average is the sum of all elements in the list divided by the number of elements in said list
            print(f"Average of all numbers entered is {avg}")
            break
        else:
            print("Give me at least one number that's not -1")
            continue # emp is empty we need at least one number to calculate the average so we go back to the beginning of the loop
Answered By: Diurnambule
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.