123k views
2 votes
Write a code segment that uses a while loop, the input function and any other necessary code to efficiently accumulate the total of the numbers inputted by the user that are between or including 5 and 9. Trace the code assuming the user inputs the values 6, 9, 3, 5 and -1 where -1 is a sentinel value that causes the loop to terminate.

User Joerno
by
4.3k points

1 Answer

1 vote

In python:

total = 0

while True:

number = int(input("Enter a number: "))

if 5 <= number <= 9:

total += number

elif number == -1:

break

print(total)

User Gianni
by
4.1k points