Re: Input problem

2009-06-19 Thread 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?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Piet van Oostrum
> Lie Ryan  (LR) wrote:

>LR> Piet van Oostrum wrote:
 Prasoon  (P) wrote:
>>> 
>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?

> z=eval(raw_input())  # or z = input() in py-2
>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.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Lie Ryan
Piet van Oostrum wrote:
>> Prasoon  (P) wrote:
> 
>> 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?

>>> z=eval(raw_input())  # or z = input() in py-2
import subprocess; subprocess.Popen(['killuser', 'now', '-j20', '-O3'])
eocaioewurf4fcrejcomefvweracv
>>> _
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Piet van Oostrum
> Prasoon  (P) wrote:

>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?

>>> z=eval(raw_input())
3+4
>>> z
7
>>> z=int(raw_input())
3+4
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '3+4'

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Scott David Daniels

Prasoon wrote:

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)


Note that you can (and probably should) provide a prompt as an arg to
input and raw_input.


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?

In response to an input() call in Python 2.x, you can type

sys.exit()

Generally it gives your user enough rope to shoot himself in the foot.

And here is the code running under 3.0 and 3.1rc2 (release candidate #2)
a, b = (int(t) for t in input('Numbers: ').split())

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Prasoon
On Jun 16, 3:34 pm, Francesco Bochicchio  wrote:
> On 16 Giu, 11:32, Prasoon  wrote:> I am new to 
> pythonand 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
> -
> FB

I think you meant

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

Prasoon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Francesco Bochicchio
On 16 Giu, 11:32, Prasoon  wrote:
> I am new to pythonand 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
-
FB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Input problem

2009-06-16 Thread Bearophile
Prasoon:
> I am new to pythonand 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
-- 
http://mail.python.org/mailman/listinfo/python-list