Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我试图在用户只单击enter时停止循环?_Python_Python 3.x - Fatal编程技术网

Python 我试图在用户只单击enter时停止循环?

Python 我试图在用户只单击enter时停止循环?,python,python-3.x,Python,Python 3.x,当用户输入一个数字时,我试图停止这个循环,调用input()和int(),这两个步骤是分开的 while True: # call input() by itself, without calling int() answer = input('Type a number, [to stop type 0 or less]') # if the user pressed enter without typing an answer, break if not a

当用户输入一个数字时,我试图停止这个循环,调用
input()
int()
,这两个步骤是分开的

while True:
    # call input() by itself, without calling int()
    answer = input('Type a number, [to stop type 0 or less]')

    # if the user pressed enter without typing an answer, break
    if not answer:
        break

    # otherwise convert answer to an integer
    number = int(answer)

    # remaining code is unchanged

告诉您一个空字符串(当只按enter键时返回)的计算结果为False,这可能是有用的。不管怎样,我还是投了赞成票
while True:
    # call input() by itself, without calling int()
    answer = input('Type a number, [to stop type 0 or less]')

    # if the user pressed enter without typing an answer, break
    if not answer:
        break

    # otherwise convert answer to an integer
    number = int(answer)

    # remaining code is unchanged