Python Input from keyboard

U

utab

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
 
M

MonkeeSage

utab said:
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

Regards,
Jordan
 
E

Eric

utab said:
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



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.
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top