simple perl program in python gives errors

Discussion in 'Python' started by squash@peoriadesignweb.com, Jan 30, 2006.

  1. Guest

    I am a little annoyed at why such a simple program in Perl is causing
    so much difficulty for python, i.e:

    $a += 200000 * 140000;

    print $a;
     
    , Jan 30, 2006
    #1
    1. Advertising

  2. kishkin Guest

    Maybe you should initialize a before using it? :)
     
    kishkin, Jan 30, 2006
    #2
    1. Advertising

  3. On 2006-01-30, <> wrote:

    > I am a little annoyed at why such a simple program in Perl is causing
    > so much difficulty for python,


    Ah, well, there's your problem. Python doesn't run Perl
    programs. It runs Python programs. If you write your program
    in Python instead of Perl, you'll not have such problems.

    --
    Grant Edwards grante Yow! If this was a SWEDISH
    at MOVIE, I'd take off your
    visi.com GO-GO BOOTS!!
     
    Grant Edwards, Jan 30, 2006
    #3
  4. On Monday 30 January 2006 17:52, wrote:
    > I am a little annoyed at why such a simple program in Perl is causing
    > so much difficulty for python, i.e:
    >
    > $a += 200000 * 140000;
    >
    > print $a;


    a = 0
    a += 200000 * 140000
    print a

    Seems to be more a problem for you than for Python.

    Christoph

    P.S.: Xah, is that you?
    P.P.S.: You should have told what's going wrong.
    P.P.P.S.: Variables are not automatically initialised in Python.
    And you shouldn't even rely on that in Perl.
    --
    Never trust a system administrator who wears a tie and suit.
     
    Christoph Haas, Jan 30, 2006
    #4
  5. kishkin Guest

    Of course Grant Edwards is right, but I thought that your programm is
    something like this:

    ------------------------
    a += 200000 * 140000
    print a
    ------------------------

    and I thought that this is your perl program:
    --------------------
    $a += 200000 * 140000
    print $a
    --------------------

    Funny if Grant Edwards is right... :)
     
    kishkin, Jan 30, 2006
    #5
  6. Guest

    i was hoping one didnt have to initialize variables because perl
    defaults their value to zero. Also I noticed if I initialize a variable
    as 0 , then I can only do integer math not floating math. this just
    seems kind of backward as I am used to php and perl which dont require
    such strict rules.
     
    , Jan 30, 2006
    #6
  7. Steve Holden Guest

    wrote:
    > I am a little annoyed at why such a simple program in Perl is causing
    > so much difficulty for python, i.e:
    >
    > $a += 200000 * 140000;
    >
    > print $a;
    >

    Yes, an annoying feature of the Python interpreter is that it doesn't
    always interpret Perl correctly.

    You will find, though, that

    print 200000 * 140000

    works quite well in both languages (with or without the trailing
    semi-colon).

    Variables in Python don't need a $ or @ to indicate whether they are
    scalar or structured - that depends on their value.

    So

    a = 0
    a += 200000 * 140000

    should also work quite well. Note the first assignment is needed because
    while there's no need to declare variables, you cannot assume a value
    until they have been assigned one (in Pythonic jargon "bound to a value").

    What does "+=" mean in Perl, by the way? Does it just assume $a is zero
    if it isn't currently existent?

    happily-forgotten-perl-ly y'rs - steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Jan 30, 2006
    #7
  8. John Zenger Guest

    wrote:
    > Also I noticed if I initialize a variable
    > as 0 , then I can only do integer math not floating math. this just
    > seems kind of backward as I am used to php and perl which dont require
    > such strict rules.
    >


    Not quite:

    >>> foo = 0
    >>> foo += 122
    >>> print foo

    122
    >>> print foo / 7

    17
    >>> print foo / 7.0

    17.4285714286
     
    John Zenger, Jan 30, 2006
    #8
  9. On 2006-01-30, <> wrote:

    > i was hoping one didnt have to initialize variables because perl
    > defaults their value to zero.


    Repeat after me: "Python is not Perl. Python is not Perl.
    Python is not Perl. Python is not Perl. Python is not Perl.
    Python is not Perl. Python is not Perl.Python is not Perl.
    Python is not Perl. Python is not Perl. Python is not
    Perl.Python is not Perl. Python is not Perl."

    > Also I noticed if I initialize a variable
    > as 0 , then I can only do integer math not floating math.


    Python is a strictly typed language. Perl isn't -- Perl does
    all sorts of stuff "automagically" by trying to guess what you
    wanted". I perfer languages that do exactly what I tell them
    to rather than what the language's author thought I might have
    meant.

    > this just seems kind of backward as I am used to php and perl
    > which dont require such strict rules.


    Really, and Perl/PHP seemed so primitive and backward to me. I
    guess it's all in the perspective.

    --
    Grant Edwards grante Yow! Darling, my ELBOW
    at is FLYING over FRANKFURT,
    visi.com Germany...
     
    Grant Edwards, Jan 30, 2006
    #9
  10. Tom Anderson Guest

    On Mon, 30 Jan 2006, Grant Edwards wrote:

    > On 2006-01-30, <> wrote:
    >
    >> i was hoping one didnt have to initialize variables because perl
    >> defaults their value to zero. Also I noticed if I initialize a variable
    >> as 0 , then I can only do integer math not floating math.

    >
    > Python is a strictly typed language. Perl isn't -- Perl does all sorts
    > of stuff "automagically" by trying to guess what you wanted". I perfer
    > languages that do exactly what I tell them to rather than what the
    > language's author thought I might have meant.


    Especially when that's Larry Wall ... :)

    tom

    --
    Don't trust the laws of men. Trust the laws of mathematics.
     
    Tom Anderson, Jan 30, 2006
    #10
  11. morris carre Guest

    a écrit :
    > I am a little annoyed at why such a simple program in Perl is causing
    > so much difficulty for python, i.e:
    >
    > $a += 200000 * 140000;
    >
    > print $a;
    >


    a = 200000 * 140000
    print a



    where's the problem ?
     
    morris carre, Jan 31, 2006
    #11
  12. Dan Lowe Guest

    On Jan 31, 2006, at 8:28 AM, morris carre wrote:

    > a écrit :
    >>
    >> $a += 200000 * 140000;
    >> print $a;

    >
    > a = 200000 * 140000
    > print a
    >
    > where's the problem ?


    Not sure if you typo'd that, but that should read:

    a += 200000 * 140000
    print a

    -dan

    --
    I am not a vegetarian because I love animals; I am a vegetarian because
    I hate plants. -A. Whitney Brown
     
    Dan Lowe, Jan 31, 2006
    #12
  13. Tim Roberts Guest

    Dan Lowe <> wrote:
    >
    >Not sure if you typo'd that, but that should read:
    >
    >a += 200000 * 140000
    >print a


    Did you try to run that?
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Feb 1, 2006
    #13
  14. Dan Lowe Guest

    On Feb 1, 2006, at 7:59 AM, Tim Roberts wrote:

    > Dan Lowe <> wrote:
    >>
    >> Not sure if you typo'd that, but that should read:
    >>
    >> a += 200000 * 140000
    >> print a

    >
    > Did you try to run that?


    Well, I did, but I had given 'a' a value first. And now I'm thinking
    that I may have forgotten some of the earlier thread... where I
    suppose the point was that this doesn't work when 'a' hasn't already
    been set. I was focusing on = vs += ...

    So in that case, please ignore me :)

    -dan

    --
    Black holes are where God divided by zero. -Steven Wright
     
    Dan Lowe, Feb 1, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,035
    Mark Goldin
    Jan 17, 2004
  2. Jonas
    Replies:
    2
    Views:
    2,858
    KevinAndrewLipscomb
    Dec 2, 2010
  3. Alan Silver
    Replies:
    1
    Views:
    348
    Alan Silver
    Feb 23, 2006
  4. Shuaib
    Replies:
    1
    Views:
    1,072
    Shuaib
    Aug 19, 2006
  5. Replies:
    5
    Views:
    257
    Anthra Norell
    Sep 4, 2006
Loading...

Share This Page