How to sum numbers from input?

前端 未结 2 1992
天涯浪人
天涯浪人 2021-01-29 13:40

I am working on the following problem.

Write a program that continually prompts for positive integers and stops when the sum of numbers entered exceeds 1000. But my cod

2条回答
  •  抹茶落季
    2021-01-29 14:06

    In case you want to stop as soon as you encounter the negative number.

    x = 0
    total = 0
    sum = 0
    while (sum <= 1000):
        x = (int(input("Enter an integer:"))
        if (x < 0):
            break
        else:
            sum += x
    

提交回复
热议问题