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 add numbers 1 to n (n is an input number) using Python

Hi I want to add numbers 1-n using while :
Fore example: if n is 3 I want to print 6
6 means: 3+2+1 (but I don’t want to show 3+2+1, I just want to show 6 in my result)

n = int(input()) 
while n>0:

I don’t know what I have to type after while n>0:

I’m new in Python and also I’m not good at English. Please help me. Thank you.

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

>Solution :

n = int(input())
my_sum = 0
while n > 0:
    my_sum += n
    n -= 1
print(my_sum)
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