Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Sum of postive numbers in python

Python program to get input n and n positive integers (skip the negative number), then compute the sum of positive integers

Sample Input 1:

5 6 7 2 -8 1 8

Sample Output 1:

24

Program or Solution

					    
							
sum1=0
num=int(input("Enter number:"))
while(num>=0):
    sum1+=num
    num=int(input("Enter number:"))
print(sum1)

    

					    
					

Program Explanation

since 1 is always true, input() statement gets input from user continously, if user enters a negative number it exit from while loop using break statement.