Python-循环输入 [英] Python - Looping an Input

查看:59
本文介绍了Python-循环输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
python,关于循环的基本问题
如何重复raw_input直到我想退出?

请给我一些Python帮助.

I would like some help with Python please.

我正在用Py2.7.2编写程序,但是遇到了一些问题.

I'm writing a program in Py2.7.2, but am having some issues.

到目前为止,我所拥有的是这样的:

What I have so far is something like this:

choice = raw_input("What would you like to do")
 if choice == '1':
  print("You chose 1")
 elif choice == '2':
  print("You chose 2")
 elif choice == '3':
  print("You chose 3")
 else:
  print("That is not a valid input.")

但是在用户选择1、2、3或4之后,程序将自动退出.有没有一种方法可以使程序循环备份,以便再次询问他们您想做什么?";直到用户退出程序为止.

But after the user chooses either 1, 2, 3 or 4, the program automatically exits. Is there a way that I can loop the program back up so that it asks them again "What would you like to do?"; and so that this continues to happen, until the user exits the program.

推荐答案

您可以使用while循环来实现.更多信息在这里: http://wiki.python.org/moin/WhileLoop

You can accomplish that with a while loop. More info here: http://wiki.python.org/moin/WhileLoop

示例代码:

choice = ""

while choice != "exit":
    choice = raw_input("What would you like to do")
    if choice == '1':
        print("You chose 1")
    elif choice == '2':
        print("You chose 2")
    elif choice == '3':
        print("You chose 3")
    else:
        print("That is not a valid input.")

这篇关于Python-循环输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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