472,328 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Python Input from keyboard


hi,
I could not understand why python stdin and stdout are not explained in
any of the tutorials on the net,

I want to read some input continuously from keyboard and then I would
like to process these input.

I have a code like this but getting errors, I would like to terminate
when there is an empty string in the input, why is not this easy as the
"cin" or "scanf". I had to search for even this easy operation. Is
there a way to send EOF signal to terminate input(ctrl+??????)

#!/usr/bin/env python
import sys, math # load system and math module
x=[]
y=[]
IN=True
print 'input x and y values : '
while IN:
xt,yt=input()

if (xt==' ' or yt==' '):
print 'you have not entered x or y, quiting'
break
else:
xt= float(xt);yt=float(yt)
x.append(xt);y.append(yt)

for i in range(x):
print x[i]

Sep 22 '06 #1
3 27741
utab wrote:
I want to read some input continuously from keyboard and then I would
like to process these input.

I have a code like this but getting errors, I would like to terminate
when there is an empty string in the input, why is not this easy as the
"cin" or "scanf". I had to search for even this easy operation. Is
there a way to send EOF signal to terminate input(ctrl+??????)
You can send a signal (check the signal module in the docs), but you
can also just do what you wanted:

x = []
y = []
while True:
msg = 'input x and y values : '
uin = raw_input(msg).strip()
if not uin:
print 'you have not entered x or y, quiting'
break
else:
xt, yt = uin.split(' ', 1)
x.append(float(xt))
y.append(float(yt))

for i in range(len(x)):
print x[i]

Regards,
Jordan

Sep 22 '06 #2
utab wrote:
hi,
I could not understand why python stdin and stdout are not explained in
any of the tutorials on the net,

I want to read some input continuously from keyboard and then I would
like to process these input.

I have a code like this but getting errors, I would like to terminate
when there is an empty string in the input, why is not this easy as the
"cin" or "scanf". I had to search for even this easy operation. Is
there a way to send EOF signal to terminate input(ctrl+??????)

#!/usr/bin/env python
import sys, math # load system and math module
x=[]
y=[]
IN=True
print 'input x and y values : '
while IN:
xt,yt=input()

if (xt==' ' or yt==' '):
print 'you have not entered x or y, quiting'
break
else:
xt= float(xt);yt=float(yt)
x.append(xt);y.append(yt)

for i in range(x):
print x[i]

Summary:

std::cout << "Statement\n";
is
print "Statement"

std::cin << value;
is
value = raw_input()
std::cout << "Prompt:"
std::cin << value;
is
value = raw_input("Prompt:")
It's a little assymetrical, but useful. Lower-level functions also
exist in the sys module as stdin, stdout and stderr.

Sep 23 '06 #3
std::cin << value;
Oops, that should be >>.

Sep 23 '06 #4

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

Similar topics

1
by: tjland | last post by:
Can python monitor keyboard input for lets say i want to have my program return to the main menu when the user hits escape! Can python do this or...
12
by: Jay | last post by:
ok, i thought for 2 seconds i might have created a Keylogger in python but i still have one major think stopping me... PYTHON. when i run the...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I...
16
by: dfaber | last post by:
Hi all, I have been searching for a keyboard and mouse tracker on linux. I've read solutions (watch at sourceforge) which look at /proc/interrupts...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use...
1
by: Troudeloup | last post by:
can python send keyboard and mouse events? like, i want to type hello 10 times in my text editor. i am on this only: windows vanilla...
1
by: Chris Carlen | last post by:
Hi: I'm writing a Python program, a hex line editor, which takes in a line of input from the user such as: -e 01 02 "abc def" 03 04 ...
20
by: Jimmy | last post by:
Hi to all python now has grown to a versatile language that can accomplish tasks for many different purposes. However, AFAIK, little is known...
1
by: john.sasil | last post by:
hi i want to play alarm sound when i press a particular key in keyboard.so someone help me in doing it. Thanks and Regards Sasil.G
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.