用户按 ENTER 键退出 while 循环
我是一个 python 新手,被要求进行一个练习:让程序循环,直到用户仅点击
请求退出。到目前为止,我已经:
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
我已经尝试过:(按照练习中的指示)
if User == <Carriage return>
,但这
if User == <Return>
只会导致无效的语法。
我如何以最简单的方式做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我在(没有双关语)寻找其他东西时遇到了这个页面。这是我使用的:
I ran into this page while (no pun) looking for something else. Here is what I use:
正是您想要的东西;)
来自 答案 /limasxgoesto0">limasxgoesto0 关于“按 Enter 键退出 while 循环而不阻塞”。
The exact thing you want ;)
from answer by limasxgoesto0 on "Exiting while loop by pressing enter without blocking. How can I improve this method?"
实际上,我想您正在寻找一个运行循环直到从键盘上按下某个键的代码。当然,程序不应该一直等待用户输入。
raw_input()
或在 python 3.0 中使用input()
,程序将等待用户按键。msvcrt
模块中的函数。实际上,ActiveState 中有一个解决这个问题的方法。请点击此链接
我认为以下链接也将帮助您更好地理解。
python 跨平台监听按键
如何获取一次按下一个按键
MS VC++ 运行时的有用例程
我希望这可以帮助您完成工作。
Actually, I suppose you are looking for a code that runs a loop until a key is pressed from the keyboard. Of course, the program shouldn't wait for the user all the time to enter it.
raw_input()
in python 2.7 orinput()
in python 3.0, The program waits for the user to press a key.kbhit()
function inmsvcrt
module.Actually, there is a recipe in ActiveState where they addressed this issue. Please follow this link
I think the following links would also help you to understand in much better way.
python cross platform listening for keypresses
How do I get a single keypress at a time
Useful routines from the MS VC++ runtime
I hope this helps you to get your job done.
这适用于使用并行线程的 python 3.5。您可以轻松地调整它,使其仅对特定的击键敏感。
This works for python 3.5 using parallel threading. You could easily adapt this to be sensitive to only a specific keystroke.
使用 print 语句查看当您按
enter
时raw_input
返回什么。然后改变你的测试来与之比较。Use a print statement to see what
raw_input
returns when you hitenter
. Then change your test to compare to that.您需要找出当您按下 Enter 键时变量 User 会是什么样子。我不会给你完整的答案,但会给你一个提示:解雇一名口译员并尝试一下。这并不难;) 请注意,默认情况下 print 的 sep 是 '\n' (是不是太多了 :o)
You need to find out what the variable User would look like when you just press Enter. I won't give you the full answer, but a tip: Fire an interpreter and try it out. It's not that hard ;) Notice that print's sep is '\n' by default (was that too much :o)
你快到了。完成此操作的最简单方法是搜索一个空变量,这就是在输入请求中按 Enter 时得到的结果。我下面的代码是3.5
You are nearly there. the easiest way to get this done would be to search for an empty variable, which is what you get when pressing enter at an input request. My code below is 3.5
我建议使用 u\000D。它是 unicode 中的 CR。
I recommend to use u\000D. It is the CR in unicode.
not user_input
检查用户是否按下了 Enter 键而没有输入数字。int(user_input) <= 0
检查用户是否输入了小于或等于零的数字。not user_input
checks if user has pressed enter key without entering number.int(user_input) <= 0
checks if user has entered any number less than or equal to zero.这是一个有效的解决方案(类似于原始问题):
请注意,原始问题中的代码有几个问题:
if
/else
位于 while 循环之外,因此循环将永远运行。else
缺少冒号。if
子句执行break
。Here's a solution (resembling the original) that works:
Note that the code in the original question has several issues:
if
/else
is outside the while loop, so the loop will run forever.else
is missing a colon.if
clause performs abreak
.一个非常简单的解决方案是,我看到你说过你
希望看到最简单的解决方案。
停止循环后提示用户继续等等。
a very simple solution would be, and I see you have said that you
would like to see the simplest solution possible.
A prompt for the user to continue after halting a loop Etc.
这是最好和最简单的答案。使用 try 和 except 调用。
Here is the best and simplest answer. Use try and except calls.
如果您希望用户按 Enter 键,则
raw_input()
将返回""
,因此请将User
与"" 进行比较:
If you want your user to press enter, then the
raw_input()
will return""
, so compare theUser
with""
:以下是我的作品:
The following works from me: