[Tutor] enhanced subtration in an exponent

Alan Gauld alan.gauld at btinternet.com
Tue Apr 21 10:14:29 CEST 2015


On 21/04/15 01:44, Jim Mooney wrote:
> Why does the compiler choke on this? It seems to me that the enhanced
> subtraction resolves to a legitimate integer

That's your mistake. Assignment is a statement not an expression. It 
does not return anything. In fact its just illegal to try:

 >>> print x=3
   File "<stdin>", line 1
     print x=3
            ^
SyntaxError: invalid syntax
 >>>

You have to do all the assignments up front.
If you don;t need to store the new value you can just
put the subtraction as an argument:

    sum += int(char) * 2**(start-1)

But if you need start to hold that new value you must
do that assignment before using it.

BTW.
This is 'A Good Thing' since it avoids a whole class
of bug that is very common in languages like C and
encourages breaking up complex statements into
multiple lines which aids debugging, readability
and maintenance.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list