Python:如何在获得特定输入之前继续重复程序?

use*_*494 9 python loops for-loop while-loop

我有一个评估输入的函数,我需要继续询问他们的输入并进行评估,直到他们输入一个空行.我怎么设置它?

while input != '':
    evaluate input
Run Code Online (Sandbox Code Playgroud)

我想过用这样的东西,但它并没有完全奏效.有帮助吗?

iCo*_*dez 18

有两种方法可以做到这一点.首先是这样的:

while True:             # Loop continuously
    inp = raw_input()   # Get the input
    if inp == "":       # If it is a blank line...
        break           # ...break the loop
Run Code Online (Sandbox Code Playgroud)

第二个是这样的:

inp = raw_input()       # Get the input
while inp != "":        # Loop until it is a blank line
    inp = raw_input()   # Get the input again
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用的是Python 3.x,则需要替换raw_inputinput.