Can someone please help me with this question, still new at while loops

May 2, 2019 180 views
Python

● Create a new file called while.py 
 
● 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. 
 
● Make use of the while loop repetition structure to implement the program. 
 
● Compile, save and run your file. 

1 Answer
Mohsen47 May 2, 2019
Accepted Answer

@jessetrading1993

It is unusual to have such kind of questions here but I will give you an answer, write this in your new file and execute it with python3 while.py

sum = 0
i = 0
while True:
        n = int(input("Input a number "))
        if n == -1:
                break
        sum += n
        i += 1

print("Average is {}".format(sum/i))

To learn about python I highly recommend this book about Python programming It is a collection of all python related tutorials in our community.

Hope this helps

https://assets.digitalocean.com/books/python/how-to-code-in-python.pdf

Have another answer? Share your knowledge.