从用户处接收整数,直到它们输入0 [保留]

问题描述 投票:-2回答:3

这是我用Python进行编程的第二周,之前从未编程过任何东西。逐步欣赏。

我不知道从哪里开始。

python python-3.x
3个回答
-1
投票
try:
    count = 0
    while True:
        user = int(input('Insert Number: '))
        count += user
        if user == 0:
            break
    print(count)
except ValueError:
    print('Please Insert Numbers Only!')

0
投票

这里开始,使用while循环作为输入。除非您需要进一步的帮助,否则我将为您提供总结部分:

cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
while cc != 0:
    cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))

0
投票
def is_int(num):
    try:
        return int(num)
    except:
        return False

total = 0
while True:
    in_user = is_int(input("input integer: "))
    if in_user is not False:
        total += in_user
    if in_user is 0:
        break

print(total)
© www.soinside.com 2019 - 2024. All rights reserved.