Question

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

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


Submit an answer

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!

Sign In or Sign Up to Answer

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.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.