469,890 Members | 1,853 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

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 27643
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by tjland | last post: by
12 posts views Thread by Jay | last post: by
4 posts views Thread by jas | last post: by
16 posts views Thread by dfaber | last post: by
12 posts views Thread by adamurbas | last post: by
1 post views Thread by Troudeloup | last post: by
20 posts views Thread by Jimmy | last post: by
1 post views Thread by john.sasil | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.