Write a Python program to find sum of 10 numbers until user enters positive numbers. In this Python example, for loop allows the user to enter 10 numbers, and the if condition checks for negative numbers. Once the user enters a negative value condition will return true, and the break statement will exit the loop.
Sum = 0 print("Please Enter 10 Numbers\n") for i in range(1, 11): num = int(input("Number %d = " %i)) if num < 0: break Sum = Sum + num print("The Sum of Positive Numbers = ", Sum)
This Python program finds the sum of 10 numbers until the user enters positive numbers using a while loop.
Sum = 0 print("Please Enter 10 Numbers\n") i = 1 while(i <= 10): num = int(input("Number %d = " %i)) if num < 0: break Sum = Sum + num i = i + 1 print("The Sum of Positive Numbers = ", Sum)
Please Enter 10 Numbers
Number 1 = 23
Number 2 = 90
Number 3 = 98
Number 4 = 56
Number 5 = 47
Number 6 = -90
The Sum of Positive Numbers = 314