Problems with user input

  • Thread starter Florian Wilhelm
  • Start date
F

Florian Wilhelm

I have some easy questions, but I cant figure them out:

I want to read exactly one char from stdin, there should
be no need to hit enter.

import sys

print "Input: (y/N) ",
input = sys.stdin.read(1)
print "Your input:", input

This code has following problems:
1) I need to hit enter, although I read only one char?!?
2) The complete output is:

Input: (y/N) y
Your input: y

Why does the whitespace char appear in front of "Your input..."?
I guess it has something to do with not causing a linebreak
after "Input: (y/N) ", but I want no linebreak...

Any suggestions?

Regards,
Florian
 
F

F. Petitjean

I have some easy questions, but I cant figure them out:

I want to read exactly one char from stdin, there should
be no need to hit enter.

import sys

print "Input: (y/N) ",
input = sys.stdin.read(1)
print "Your input:", input

This code has following problems:
1) I need to hit enter, although I read only one char?!?
sys.stdin is : buffered and line oriented.
What is your operating system ? Under unix sys.stdin (and C stdin
stream) are managed by a driver which expect you can somewhat modify
the line you want to send to the system. (POSIX normalized)
If you want a fine control, you can use curses (Unix) or build your
solution based on getchar (or is it getch() ?) on windows/dos.
2) The complete output is:

Input: (y/N) y
Your input: y

Why does the whitespace char appear in front of "Your input..."? ??
I guess it has something to do with not causing a linebreak
after "Input: (y/N) ", but I want no linebreak...

Any suggestions?
Tell us more about what you want to do.
 
D

Diez B. Roggisch

I want to read exactly one char from stdin, there should
be no need to hit enter.

That's because your terminal is in canonical mode - that means that it only
reads whole lines. You can put it to non-canonical mode - one way is
described here:


http://www.ibiblio.org/obp/py4fun/lode/lode.html#auto2

Please make sure you set it back to canonical mode, or you will expirience
some strange effects.
Why does the whitespace char appear in front of "Your input..."?
I guess it has something to do with not causing a linebreak
after "Input: (y/N) ", but I want no linebreak...

The print statement concats an argument list by spaces. Use this instead:

sys.stdout.write("Your input:%s\n" % input)
 
F

Florian Wilhelm

My OS is Linux 2.6.8.1 and Python version 2.3.4

I just want to write a function that, if called,
asks a yes/no question. The user should only need
to type one char, without confirming with enter.

I wrote a function similar to following

import sys

print "Input: (y/N) ",
input = sys.stdin.read(1)
print "Your input:", input

If I execute these lines and type "y" followed by "enter".
I get this output

Input: (y/N) y
Your input: y

The problem is that there is a whitespace in front of the
second line, that is not generated by the second print
statement. I dont understand why it appears...

It really has nothing to do with the second "print",
because following code:

import sys

print "Input: (y/N) ",
input = sys.stdin.read(1)
print "Hello World"
print "Your input:", input

produces this output...

Input: (y/N) y
Hello World
Your input: y

Can somebody explain why the whitespace char appears
in front of "Hello World"???
 
S

Steven Bethard

Florian Wilhelm said:
import sys

print "Input: (y/N) ",
input = sys.stdin.read(1)
print "Hello World"
print "Your input:", input

produces this output...

Input: (y/N) y
Hello World
Your input: y

Can somebody explain why the whitespace char appears
in front of "Hello World"???

This is the behavior of the print statement. For example:
X1
1 Y

Every comma between two expressions in the print statement adds a space. As you
can see from the code above, the space isn't inserted until after the following
expression (sys.stdin.read(1)) is executed, and hence I get the space from the
print statement following the newline I inserted when I pressed <ENTER> after
typing 1.

If instead, you use sys.stdout.write, you get:
.... sys.stdout.write("X%s" % sys.stdin.read(1))
.... sys.stdout.write("Y\n")
....1
X1Y

sys.stdout.write doesn't insert spaces (unless you put spaces in the strings).
In general, if you want a particular format, you should use sys.stdout.write
instead of print, since the print statement has a number of such idiosyncracies.

Steve
 
T

Terry Reedy

Florian Wilhelm said:
I want to read exactly one char from stdin, there should
be no need to hit enter.

Your problem is that terminal input, as seen from C, is usually, by
default, in buffered line mode. This allows the user to edit entries, at
least by back-spacing, before hitting enter. How to switch to character
mode or grab the raw keyboard input is OS specific.
Input: (y/N) y
Your input: y

Why does the whitespace char appear in front of "Your input..."?

Because the print statement puts spaces between items so you don't have to.
If the convenience of print does not do what you want, take control by
using sys.stdout.write(<string>), which writes *exactly* what you tell it
to.

Terry J. Reedy
 

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

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top