Report this

What is the reason for this report?

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

Posted on May 2, 2019

● 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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

@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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.