Python:如何不断重复程序直到获得特定输入? [英] Python: How to keep repeating a program until a specific input is obtained?
本文介绍了Python:如何不断重复程序直到获得特定输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个评估输入的函数,我需要不断询问他们的输入并评估它,直到他们输入一个空行.我该如何设置?
I have a function that evaluates input, and I need to keep asking for their input and evaluating it until they enter a blank line. How can I set that up?
while input != '':
evaluate input
我想过使用类似的东西,但它并没有完全奏效.有什么帮助吗?
I thought of using something like that, but it didn't exactly work. Any help?
推荐答案
有两种方法可以做到这一点.首先是这样的:
There are two ways to do this. First is like this:
while True: # Loop continuously
inp = raw_input() # Get the input
if inp == "": # If it is a blank line...
break # ...break the loop
第二个是这样的:
inp = raw_input() # Get the input
while inp != "": # Loop until it is a blank line
inp = raw_input() # Get the input again
请注意,如果您使用的是 Python 3.x,则需要将 raw_input
替换为 input
.
Note that if you are on Python 3.x, you will need to replace raw_input
with input
.
这篇关于Python:如何不断重复程序直到获得特定输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文