473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with user input

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

Jul 18 '05 #1
6 2307
On Tue, 09 Nov 2004 23:54:37 Florian Wilhelm <Fl*************@web.de> wrote:
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.
Regards,
Florian

Jul 18 '05 #2
> 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)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
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"???
Jul 18 '05 #4
Florian Wilhelm <Florian.Wilhelm <at> web.de> writes:

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:
print "X", sys.stdin.read(1), "Y" 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:
def f(): .... sys.stdout.write("X%s" % sys.stdin.read(1))
.... sys.stdout.write("Y\n")
.... f()

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

Jul 18 '05 #5
Thank you very much!

That solved both problems!

Jul 18 '05 #6

"Florian Wilhelm" <Fl*************@web.de> wrote in message
news:pa****************************@web.de...
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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan Weeb | last post by:
Hi All, I have struggled through this far with help from many of you so thanks. I am stuck again. I am really new to this so don't be harsh :-) There are a few problems. You can run the script...
4
by: Eric | last post by:
Hey Everyone.. I have a form that has approximately 7 text fields and 1 checkbox. Generally when this form is submitted(to itself BTW) it works fine, however, when the checkbox is only field...
4
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
4
by: will | last post by:
Trying to modify some code from : http://www.faqts.com/knowledge_base/view.phtml/aid/2357 But having some problems. Cant work out how/why its not adding rows in the right place. seems strange to...
2
by: Joseph Chase | last post by:
I have the following form : <form action="/auction/create/" method="post"><p><label for="auction_created">Created</label><br /></p> <p><label for="auction_user">User</label><br /><input...
7
by: jmac | last post by:
Greetings fellow programmers, I have created a C program that has a few bugs and would like to get some help with working them out. Here is a list of the problems that I am experiencing: -...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
1
by: RDizzle | last post by:
okay. so all i am doing is changing a registration script that uses $_GET to a script that uses $_POST, but the validation script now returns NULL values for all posted vars. What's the deal? ...
14
by: Samuel.Coddler | last post by:
Hi, I'm a newbie programmer. I can't get work the following code. /* Objective: Use a loop to print out all of the input characters until a newline is found.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.