simple perl program in python gives errors

S

squash

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;
 
G

Grant Edwards

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.
 
C

Christoph Haas

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.
 
K

kishkin

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

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

squash

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.
 
S

Steve Holden

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
 
J

John Zenger

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:
17.4285714286
 
G

Grant Edwards

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.
 
T

Tom Anderson

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
 
M

morris carre

(e-mail address removed) 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 ?
 
D

Dan Lowe

(e-mail address removed) a écrit :

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
 
D

Dan Lowe

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top