Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sum input numbers using while loop? (python)

Beginner question, I have to create a program that asks user to input numbers (input 0 to break), then calculates the amount of numbers in total and then the sum of the input numbers.
How do i print the sum of user-input numbers using while loop? This is what I got so far

amount = 0
while True:
    amount += 1
    number = int(input("Number: "))
    if number == 0:
        break
    
print(f"Numbers in total: {amount-1}")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are close. Same as you have amount = 0, create a variable total = 0. And, inside the loop, add total += number, after the line where you are reading it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading