Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

exist loop by pressing esc

116 views
Skip to first unread message

chris alindi

unread,
Oct 23, 2016, 5:34:53 PM10/23/16
to
simple while loop range(10) if user press esc exits loop

Steve D'Aprano

unread,
Oct 23, 2016, 9:36:40 PM10/23/16
to
On Mon, 24 Oct 2016 08:34 am, chris alindi wrote:

> simple while loop range(10) if user press esc exits loop


Your post is not a question nor even a grammatical sentence? Would you like
us to guess what you mean? Or perhaps you could ask your question in actual
proper sentences?

If English is not your first language, please say so, and try your best.

It may help if you show some code, and explain what result you want.



--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

Wildman

unread,
Oct 24, 2016, 12:19:07 AM10/24/16
to
On Sun, 23 Oct 2016 14:34:29 -0700, chris alindi wrote:

> simple while loop range(10) if user press esc exits loop

If I understand you correctly you want to exit a while loop
with the ESC key. That can be done but it depends on the
platform. For Windows use this: (not tested)

import msvcrt

while something:
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break


In Linux you need to install getch:

https://pypi.python.org/pypi/getch

Download it here:

https://pypi.python.org/pypi/getch#downloads

Extract the tar.gz file. A directory called getch-1.0
will be created. Open a terminal in that directory and
enter this...

sudo python setup.py install

Here is a code example: (tested)

import getch

while something:
if getch.getch() == '\x1b':
break

--
<Wildman> GNU/Linux user #557453
May the Source be with you.

John Gordon

unread,
Oct 24, 2016, 11:06:11 AM10/24/16
to
In <6e030fd0-93c1-4d23...@googlegroups.com> chris alindi <alindi...@gmail.com> writes:

> simple while loop range(10) if user press esc exits loop

range() is typically used with for loops, not while loops.

what is your while condition?
what use is the range() value?

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

0 new messages