eric3 question

  • Thread starter Alexander Zatvornitskiy
  • Start date
A

Alexander Zatvornitskiy

Hello All!

I'am using eric3 IDE under win32 (snapshot 2005-04-10), and have a trouble. I
use this code:
print "enter q to quit, or smthing else to continue"
while not sys.stdin.readline()=="q":
smthing(else)

I can't run this code to debug in eric3. The questions are - how to fix it and
how to ask user for press a key without troubles with eric?

msvcrt.kbhit don't work also.

Thank you in advance

Alexander, (e-mail address removed)
 
G

Gerrit van Dyk

Alexander said:
Hello All!

I'am using eric3 IDE under win32 (snapshot 2005-04-10), and have a trouble. I
use this code:
print "enter q to quit, or smthing else to continue"
while not sys.stdin.readline()=="q":
smthing(else)

Try using raw_input() instead of the sys.stdin.readline(). raw_input()
is the preferred way of getting console input.

Like this:
print "enter q to quit, or smthing else to continue"
while not raw_input() == 'q':
smthing(else)

Or you can do it this way:
while not raw_input("enter q to quit, or smthing else to continue: ")
smthing(else)

Gerrit van Dyk
 
F

F. Petitjean

Le Tue, 31 May 2005 03:13:31 +0400, Alexander Zatvornitskiy a écrit :
Hello All!

I'am using eric3 IDE under win32 (snapshot 2005-04-10), and have a trouble. I
use this code:
print "enter q to quit, or smthing else to continue"
while not sys.stdin.readline()=="q":
smthing(else)
prompt = "enter q to quit, or smthing else to continue"
req = raw_input(prompt)
if req == "q":
raise SystemExit()
smthing(else)
 
M

Magnus Lycka

Gerrit said:
Try using raw_input() instead of the sys.stdin.readline(). raw_input()
is the preferred way of getting console input.

Is it? Guido's "Python Regrets" slides (Google for them) seems
to say the opposite. Besides, that's not the problem here...

Alexander said:
> I'am using eric3 IDE under win32 (snapshot 2005-04-10), and have a trouble. I
> use this code:
> print "enter q to quit, or smthing else to continue"
> while not sys.stdin.readline()=="q":
> smthing(else)

That will actually work in a vanilla Unix Python interpreter
if you type q followed by Ctrl-D twice, but I suspect that's
not what you intended...

The readline method includes the whole line, including the
linefeed character if you press q followed by ENTER. If you
change it to...

while not sys.stdin.readline()=="q\n":

....I guess it will work in eric3. (It works in IDLE then.)
You might even want something more robust, such as

while not sys.stdin.readline().strip().lower() == "q":

In general, your construct makes it a bit difficult for you
to debug your own code. If you had written it like below...

while True:
inp = sys.stdin.readline()
if inp == 'q':
break
smthing(else) # Not that you can use else as a name...

....it would have been trivial to insert "print repr(inp)"
which ought to have given you a clue of what was going on...

Changes and experiments like that are very easy to do in
Python. It beats staring at the code in confusion 99 times
out of 100. It's typically much faster than asking on c.l.py,
although not educational for as many people... ;)
 
A

Alexander Zatvornitskiy

ðÒÉ×ÅÔ F.!

31 ÍÁÑ 2005 × 06:08, F. Petitjean × Ó×ÏÅÍ ÐÉÓØÍÅ Ë All ÐÉÓÁÌ: FP> prompt = "enter q to quit, or smthing else to continue"
FP> req = raw_input(prompt)
FP> if req == "q":
FP> raise SystemExit()
FP> smthing(else)
Thank you, raw_input works fine in eric3.

Alexander, (e-mail address removed)
 
A

Alexander Zatvornitskiy

ðÒÉ×ÅÔ Magnus!

31 ÍÁÑ 2005 × 13:46, Magnus Lycka × Ó×ÏÅÍ ÐÉÓØÍÅ Ë All ÐÉÓÁÌ:
ML> The readline method includes the whole line, including the
ML> linefeed character if you press q followed by ENTER. If you
ML> change it to...

ML> while not sys.stdin.readline()=="q\n":
oooops. I'am usually use Ctrl+C to break my program, so this bug was not found
by me.
ML> ...I guess it will work in eric3. (It works in IDLE then.)
no. under eric3, exception "The socket operation could not completed without
blocking" on line 160 in AsincFile.py (this file is from python's
distribution). It seems, cause of bug is in method of redirection the console
input/output to eric's windows.

Btw, raw_input works fine.

ML> In general, your construct makes it a bit difficult for you
ML> to debug your own code. If you had written it like below...

ML> while True:
ML> inp = sys.stdin.readline()
ML> if inp == 'q':
ML> break
ML> smthing(else) # Not that you can use else as a name...
I hate such loops. Usually, if I see the loop, I want to see its condition at
once, I don't want to search for it. In my opinion, "while True"+"break" is
something like "goto"+"if" or even worse.
ML> Changes and experiments like that are very easy to do in
ML> Python. It beats staring at the code in confusion 99 times
ML> out of 100. It's typically much faster than asking on c.l.py,
ML> although not educational for as many people... ;)
Anyway, now you know that eric3 use (possibly) little bit buggy mechanism for
redirection of input/output. It's cool:)

Alexander, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,103
Latest member
VinaykumarnNevatia
Top