[Tutor] while/if/elif/else loops

Carroll, Barry Barry.Carroll at psc.com
Tue Nov 1 19:52:58 CET 2005


Greetings:

I tried running first just the expression, then the statement.  Here are the
results:

>>> tosses = 100<0
>>> tosses
False
>>> while tosses = 100<0:
  File "<stdin>", line 1
    while tosses = 100<0:
                 ^
SyntaxError: invalid syntax
>>>

Coming from the C/C++ world as I do,  I was a little surprised by the error.
C accepts the assignment expression and uses the result as the while
condition, so that the loop never executes.  Nice to see that Python avoids
that trap.

Regards,

Barry

> Date: Mon, 31 Oct 2005 22:10:34 -0500
> From: "R. Alan Monroe" <amonroe at columbus.rr.com>
> Subject: Re: [Tutor] while/if/elif/else loops
> To: tutor at python.org
> Message-ID: <331093283819.20051031221034 at columbus.rr.com>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> > while tosses = 100<0:
> 
> I didn't run the program, but this immediately caught my eye as
> looking kind of suspect. Was it a typo? Most programs usually
> have a need for comparisons of equal, or greater/less, but it's really
> rare to need both combined in one statement...
> 
> Also if you really _do_ want to check two things being equal, don't
> forget to use double equal ( == ).
> 
> Alan
> 



More information about the Tutor mailing list