● 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!
Accepted Answer
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.