用户按 ENTER 键退出 while 循环 [英] Exit while loop by user hitting ENTER key

查看:60
本文介绍了用户按 ENTER 键退出 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,被要求使用 while 和 for 循环进行一些练习.我被要求进行程序循环,直到用户仅点击 请求退出.到目前为止,我有:

I am a python newbie and have been asked to carry out some exercises using while and for loops. I have been asked to make a program loop until exit is requested by the user hitting <Return> only. So far I have:

User = raw_input('Enter <Carriage return> only to exit: ')
running = 1
while running == 1:
    Run my program
if User == # Not sure what to put here
    Break
else
    running == 1

我已经尝试过:(按照练习中的说明)

I have tried: (as instructed in the exercise)

if User == <Carriage return>

还有

if User == <Return>

但这只会导致无效的语法.请你告诉我如何以最简单的方式做到这一点.谢谢

but this only results in invalid syntax. Please could you advise me on how to do this in the simplest way possible. Thanks

推荐答案

我在(不是双关语)寻找其他东西时遇到了这个页面.这是我使用的:

I ran into this page while (no pun) looking for something else. Here is what I use:

while True:
    i = input("Enter text (or Enter to quit): ")
    if not i:
        break
    print("Your input:", i)
print("While loop has exited")

这篇关于用户按 ENTER 键退出 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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