99.3k views
0 votes
Write a program that continues asking the user for a number until they enter “stop”. Your program should then output the sum of all the numbers that the user entered. Hint: use a while loop

2 Answers

3 votes

Answer:

# Initialize the sum to 0

sum = 0

# Start an infinite loop

while True:

# Ask the user for a number

num = input("Enter a number (or 'stop' to finish): ")

# If the user enters "stop", break out of the loop

if num == "stop":

break

# Otherwise, add the number to the sum

sum += int(num)

# Print the final sum

print("The sum of the numbers you entered is:", sum)

User Dasha Salo
by
4.0k points
6 votes

Answer:

total = 0

while True:

user_input = input("Please enter a number: ")

if user_input == "stop":

break

total += int(user_input) # convert user_input to int before adding to total

print(f"The sum of all the numbers you entered is {total}")

User Peco
by
3.6k points