473,792 Members | 3,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2334
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.writ e("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.Wilhel m <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.writ e, you get:
def f(): .... sys.stdout.writ e("X%s" % sys.stdin.read( 1))
.... sys.stdout.writ e("Y\n")
.... f()

1
X1Y

sys.stdout.writ e doesn't insert spaces (unless you put spaces in the strings).
In general, if you want a particular format, you should use sys.stdout.writ e
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.writ e(<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
2406
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 here http://www.pbrown.com/research/test1.php to see the formatting issues Formatting Issues
4
4298
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 that has been modified/clicked the form doesn't always submit. When it does work, a Stored procedure is passed form variables and updates to the db are made. When it doesn't, its as if the form wasn't submitted, it reloads and resets the page, but...
4
2822
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 page, puts that into an array and inserts the array into SQL server. Now here is the problem:
13
40769
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 style="display:none"> can be displayed by setting the style attribute to "display:", or hidden with "display:none". This gives the illusion that the person filling out the form is switching from page to page...without the overhead of extra hits on the server,...
4
5127
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 me. Im trying to get the NS code working first since that appears to be a bit harder to figure out.. heres my code.. http://www.sourceymonkey.com/tmp/upload.html I was guessing it was something to do with var f but playing around with that...
2
2261
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 id="auction_user" name="auction" size="30" type="text" value="" /></p> <p><label for="auction_auction_title">Auction title</label><br /><input id="auction_auction_title" name="auction" size="30" type="text" value="" /></p>
7
4122
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: - The program calls for the input to be added in one line (? 100 Smith 24.98), but my right now my program skips a line each time for example: ? 100
6
2824
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 matrix by matrix (mxm) and matrix by vector (mxv) multiplication, so obviously one of my first considerations is to ask the user if they want an mxm or mxv multiplication performed. I have written the code below (this is a working snippet of the...
1
6995
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? NOTE: when i use $_GET the script just works. Thanks in advance for helping a noob.
14
3083
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
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.