Python:如何继续重复一个程序,直到获得一个特定的输入? [英] Python: How to keep repeating a program until a specific input is obtained?

查看:838
本文介绍了Python:如何继续重复一个程序,直到获得一个特定的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个评估输入的函数,我需要不断的询问他们的输入并评估它,直到输入一个空行。

  while input!='':
评估输入

code>

我曾经想过使用类似的东西,但是并不完全正确。任何帮助?

解决方案

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

  while True:#循环连续
inp = raw_input()#获取输入
if if ==:#如果是空行...
break#...打破循环

第二个是这样的:

$ p $ inp = raw_input()#获取输入
while inp!=:#循环到空行
inp = raw_input()#再次获取输入

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


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

The second is like this:

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

Note that if you are on Python 3.x, you will need to replace raw_input with input.

这篇关于Python:如何继续重复一个程序,直到获得一个特定的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆