[Tutor] while Loop
Darren Williams
D3IBZ at hotmail.com
Wed Jul 18 18:16:27 CEST 2007
Luke and Kent, you're right, I didn't think JavaScript calculated
multiplaction and division before addition and subtraction but seems it does
:)
I dunno what you mean about usedPocketsOne not being defined, didn't I
define it with usedPocketsOne = 192000?
----- Original Message -----
From: "Luke Paireepinart" <rabidpoobear at gmail.com>
To: "Darren Williams" <D3IBZ at hotmail.com>
Cc: <tutor at python.org>
Sent: Wednesday, July 18, 2007 11:57 AM
Subject: Re: [Tutor] while Loop
> Darren Williams wrote:
>> Hi all,
>> I'm writing a calculator for an online game that I used to play but
>> don't seem to be able to break out of the while loop, the program will
>> just go over and over the loop, crashing the program and I don't know if
>> Python is just really slow at this type of thing or i'm doing it
>> completely wrong in Python (the equivalent code in JavaScript works
>> fine).
> I don't think you're doing it completely wrong, just partially :)
> And I doubt that, if it's quick in Javascript, it will be any slower in
> Python, since Javascript is interpreted as well, but by the browser while
> it's simultaneously downloading the content of the webpage and rendering
> it to the screen.
> Also, I'm pretty sure the code here is not equivalent to the Javascript
> code you included (specific remarks below.)
>> Python code -
>> def main():
>> usedPocketsOne = 192000
>> junkiesOne = 500
>> labSpaceOne = 0
>> resultOne = 0
>> while usedPocketsOne > (junkiesOne - labSpaceOne) * 17:
> I don't know much about Javascript, but doesn't it have order of
> operations?
> For example,
> usedPocketsOne > junkiesOne - labSpaceOne * 17
> is generally assumed to be
> x = labSpaceOne * 17
> y = junkiesOne - x
> usedPocketsOne > y
>
>
> Whereas your Python equivalent, with the parenthesis,
> usedPocketsOne > (junkiesOne - labSpaceOne) * 17
>
> changes the order of operations (forces the subtraction before the
> multiplication)
> and hence changes the resulting value. That is, unless Javascript
> evaluates left-to-right with no operator heirarchy (which I would be
> saddened to learn, if it's true.)
>> resultOne = resultOne + 1
>> usedPocketsOne = (UsedPocketsOne - junkiesOne + labSpaceOne) * 17
> This shouldn't even run - you should get a syntax error because
> UsedPocketsOne is not defined anywhere else in the program (that you've
> shown us.)
> Perhaps you're running it in IDLE with no subprocess and some value for
> UsedPocketsOne is hanging around (from previous edits of the program, or
> interpreter session, or something else) and mucking up the works?
> -Luke
>
>
>
More information about the Tutor
mailing list