如何让 raw_input 重复直到我想退出?

发布于 2024-08-12 06:53:17 字数 233 浏览 4 评论 0原文

假设我想像这样使用 raw_input

code = raw_input("请输入您的三字母代码或空行来退出:")

下:

if __name__=="__main__": 

我怎样才能让每次运行程序时它都会重复多次而不是一次?
另一个问题是编写什么代码可以满足“或空行退出(程序)”条件。

Say I want to use raw_input like this:

code = raw_input("Please enter your three-letter code or a blank line to quit: ")

under:

if __name__=="__main__": 

How can I let it repeat multiple times rather than just once every time I run the program?
Another question is to write what code can satisfy the condition "or a blank line to quit (the program)".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2024-08-19 06:53:17

最好的:

if __name__ == '__main__':
  while True:
    entered = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
    if not entered: break
    if len(entered) != 3:
      print "%r is NOT three letters, it's %d" % (entered, len(entered))
      continue
    if not entered.isalpha():
      print "%r are NOT all letters -- please enter exactly three letters, nothing else!"
      continue
    process(entered)

best:

if __name__ == '__main__':
  while True:
    entered = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
    if not entered: break
    if len(entered) != 3:
      print "%r is NOT three letters, it's %d" % (entered, len(entered))
      continue
    if not entered.isalpha():
      print "%r are NOT all letters -- please enter exactly three letters, nothing else!"
      continue
    process(entered)
策马西风 2024-08-19 06:53:17
while 1:
    choice=raw_input("Enter: ")
    if choice in ["Q","q"]: break
    print choice
    #do something else
while 1:
    choice=raw_input("Enter: ")
    if choice in ["Q","q"]: break
    print choice
    #do something else
烟凡古楼 2024-08-19 06:53:17
def myInput():
    return raw_input("Please enter your three-letter code or a blank line to quit: ")

for code in iter(myInput, ""):
    if len(code) != 3 or not code.isalpha():
        print 'invalid code'
        continue
    #do something with the code
def myInput():
    return raw_input("Please enter your three-letter code or a blank line to quit: ")

for code in iter(myInput, ""):
    if len(code) != 3 or not code.isalpha():
        print 'invalid code'
        continue
    #do something with the code
去了角落 2024-08-19 06:53:17
if __name__ == '__main__':

    input = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
    while input:
        input = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
if __name__ == '__main__':

    input = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
    while input:
        input = raw_input("Please enter your three-letter code or leave a blank line to quit: ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文