Input problem

P

Prasoon

I am new to python....and using python 2.6
I want to know when to use raw_input( ) and when to use input( )???

According to my interpretation one should use input( ) when entering
numbers etc and
raw_input( ) when a string is too be entered.....

Correct me if I am wrong....

Also if I want to enter two numbers 'a' and b such that while entering
them through the keyboard
there is a space between the two...

For example: .....12 15

Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b'????

I mean how to handle spaces???/
 
B

Bearophile

Prasoon:
I am new to python....and using python 2.6
I want to know when to use raw_input( ) and when to use input( )???

I think it's better to always use raw_input(), and then convert the
string to the data to work on.

Once in a while you may want to use input() to input expressions (like
formulas) in your program in a quick, simple and unsafe way...

In Python3+ they have removed input() and they have renamed raw_input
() as input(). You can have the functionality of 2.x input() with eval
(input()). (I think Python3 developers have taken the right choices
here).

Bye,
bearophile
 
F

Francesco Bochicchio

I am new to python....and using python 2.6
I want to know when to use raw_input( ) and when to use input( )???

According to my interpretation one should use input( ) when entering
numbers etc and
raw_input( ) when a string is too be entered.....

Correct me if I am wrong....
You should almost always use raw_input and write your own code to
validate the
input and convert it. input (wich is roughly equivalent of veval
(raw:_input())
is officially considered a Bad Choice and as such has been changed in
Python 3.x
( that is, python 3.x 'input' is equivalent to python 2.x raw_input ).

P.S : if you are new to python and don't expect to use external
libraries for the next
months (one year?) you might consider to start directly with python
3.x.

Also if I want to enter two numbers 'a' and b such that while entering
them through the keyboard
there is a space between the two...

For example:>>>Enter two numbers:

 .....12 15

Can I use input( ) such that 12 gets accepted in 'a' and 15 in 'b'????

I mean how to handle spaces???/


For instance: map( int, raw_input.split() ) splits the
input string using blanks as separator, then try to convert each piece
in an integer
and returns a list of integer. Of course if the input string is not a
list of integer
you get an exception.

You could also do:

a, b = map( int, raw_input.split() )

but in this case you get an exception also if the input strings
cobntains less or more than two integers.

Ciao
 
P

Prasoon

You should almost always use raw_input and write your own code to
validate the
input and convert it. input (wich is roughly equivalent of veval
(raw:_input())
is officially considered a Bad Choice and as such has been changed in
Python 3.x
( that is, python 3.x 'input' is equivalent to python 2.x raw_input ).

P.S : if you are new to python and don't expect to use external
libraries for the next
months (one year?) you might consider to start directly with python
3.x.






For instance: map( int, raw_input.split() ) splits the
input string using blanks as separator, then try to convert each piece
in an integer
and returns a list of integer. Of course if the input string is not a
list of integer
you get an exception.

You could also do:

a, b =  map( int, raw_input.split() )

but in this case you get an exception also if the input strings
cobntains less or more than two integers.

Ciao

I think you meant

a, b = map( int, raw_input().split() )

Prasoon
 
P

Prasoon

What is the difference between

z=int(raw_input()) and z=eval(raw_input())????(I thought them to be
the same in case of integers)

I mean when an integer is entered in that case are they same and when
an integer in not entered,in that case how are they different?????
 
P

Piet van Oostrum

Prasoon said:
P> What is the difference between
P> z=int(raw_input()) and z=eval(raw_input())????(I thought them to be
P> the same in case of integers)
P> I mean when an integer is entered in that case are they same and when
P> an integer in not entered,in that case how are they different?????
3+4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3+4'
 
L

Lie Ryan

import subprocess; subprocess.Popen(['killuser', 'now', '-j20', '-O3'])
eocaioewurf4fcrejcomefvweracv
 
P

Piet van Oostrum

Lie Ryan said:
P> What is the difference between
P> z=int(raw_input()) and z=eval(raw_input())????(I thought them to be
P> the same in case of integers)P> I mean when an integer is entered in that case are they same and when
P> an integer in not entered,in that case how are they different????? LR> import subprocess; subprocess.Popen(['killuser', 'now', '-j20', '-O3'])
LR> eocaioewurf4fcrejcomefvweracv

SyntaxError: invalid syntax eval will not accept statements like import,
only expressions. But as Scott David Daniels already had mentioned you
can shoot yourself in the foot easily with input() or eval() if you
can't trust the input.
 

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,777
Messages
2,569,604
Members
45,216
Latest member
Best cryptoconsultant

Latest Threads

Top