In: Computer Science
Python Programming Question:
Objective: Sum of Numbers.
Design a program with a loop that asks the user to enter a series of positive numbers. The user should enter "0" (zero) to signal the end of the series. After all the positive numbers have been entered, the program should display their sum.
The program should display the following:
• Contain proper indentation.
• Comments (Blocks or Lines).
• Good Variables initializations.
• Good questions asking for the parameters of the code.
• Output display with explanation.
I am not able to have my code work properly. What is wrong with my code below?:
print("Enter positive numbers: ")
#taking integer as input
number = int (input())
#intialising sum to 0
sum = 0
#checking for negative integer
#if negative asks again input
if number<0:
print("enter positive values only")
number = int(input())
#taking positive integers untill '0' pressed
while number ! = 0 :
sum=sum+number #finding sum of entered integers
number = int(input())
if number<0:
print("enter positive values only")
number = int(input())
# sum of the integers
print("Sum of given integers is:",sum)
Explanation:
Here is the code which keeps asking for the positive values only until 0 is entered by the user.
And then all the numbers are added one by one as they are entered.
At last, the sum of the numbers is printed out.
Code:
print("Enter positive numbers: ")
#taking integer as input
number = int (input())
#intialising sum to 0
sum = 0
#checking for negative integer
#if negative asks again input
while number<0:
print("enter positive values only")
number = int(input())
#taking positive integers untill '0' pressed
while number != 0 :
sum=sum+number #finding sum of entered integers
number = int(input())
while number<0:
print("enter positive values only")
number = int(input())
# sum of the integers
print("Sum of given integers is:",sum)
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!