161k views
2 votes
Write a function that repeatedly prompts the user to enter a non-zero integer and calculates and displays the running sum of all positive integers entered so far. The loop should continue until the user enters a 0 or until the user entered 10 numbers. The function will print the final sum of all positive integers and will not return any output.

User THEMike
by
8.2k points

1 Answer

3 votes

Final answer:

To solve this problem, you can use a while loop that prompts the user for an integer and calculates the running sum of all positive integers entered.

Step-by-step explanation:

To solve this problem, you can use a while loop that will continue until the user enters a 0 or until the user has entered 10 numbers. Inside the loop, you can prompt the user for an integer and add it to a running sum variable if the entered number is positive. After the loop ends, you can print the final sum of all positive integers entered.


Here is an example of a function that implements this:


def calculate_running_sum():
sum = 0
count = 0
while count < 10:
num = int(input('Enter a non-zero integer: '))
if num == 0:
break
if num > 0:
sum += num
count += 1
print('Final sum:', sum)

User Ptriek
by
8.6k points