187k views
0 votes
Write a program that continually prompts the user to enter in a value. The user may enter any integer (positive or negative) or the string ""end"". The goal is to compute the sum of all inputted values

User T Mitchell
by
8.5k points

1 Answer

2 votes

Final answer:

To write a program that prompts the user to enter values and computes the sum, you can use a loop in a high-level programming language like Python. Here is an example.

Step-by-step explanation:

To write a program that prompts the user to enter values and computes the sum, you can use a loop in a high-level programming language like Python. Here is an example:

sum = 0
while True:
value = input('Enter a value: ')
if value == 'end':
break
sum += int(value)
print('The sum of the values entered is:', sum)

In this program, we initialize a variable sum to 0. Then, we use a while loop with a condition that is always true, so it continues until we explicitly break out of it. Inside the loop, we prompt the user to enter a value using the input function and store it in the variable value. We check if the value is equal to 'end'. If it is, we break out of the loop. Otherwise, we convert the value to an integer using the int function and add it to the sum. Finally, we print the sum.

This program will keep prompting the user for values until they enter 'end'. It will compute the sum of all the inputted values, including positive and negative integers. For example:

Enter a value: 5
Enter a value: -3
Enter a value: 10
Enter a value: end
The sum of the values entered is: 12
User Santosh Panda
by
8.4k points