Question

In: Computer Science

Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the...

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)

Solutions

Expert Solution

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!


Related Solutions

Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
Design a modular program which asks the user to enter a list of numbers. The numbers...
Design a modular program which asks the user to enter a list of numbers. The numbers must be stored in an array. The program then finds the index of the first occurrence of the smallest element in the array and the last occurrence of the largest element in the array. The program displays the position and value of each of these items.
This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
Create this on Python Write a program that asks for three numbers. Check first to see...
Create this on Python Write a program that asks for three numbers. Check first to see that all numbers are different. If they’re not different, then exit the program. Otherwise, display the largest number of the three. Outputs: - Enter the first number: 4 - Enter the second number: 78 - Enter the third number: 8 (The largest number is 78.) Tasks - Complete the algorithm manually without using any built-in functions to find the largest number on list. -...
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT