[Tutor] Assistance

Wayne Werner waynejwerner at gmail.com
Mon Apr 25 17:41:41 CEST 2011


On Mon, Apr 25, 2011 at 9:59 AM, Prasad, Ramit <ramit.prasad at jpmchase.com>wrote:

> >>    while numberOfGrades != gradesEntered:
> >>        grade = int(raw_input("Please enter the grade:" ))
> >>        gradesEntered += 1
> >>        score =+ grade
>
> >Note that += and =+ do different things. I suspect this last line is
> >not doing what you think. Details like this are very important in
> >programming, especially since both forms are valid code, they
> >just do different things!
>
> Could you please expand on that? From playing around on the shell it looks
> like 'B =+ 2 ' always sets B to 2. At first, I thought it was taking it as
> 'B=None+2' but that gave me the error "TypeError: unsupported operand
> type(s) for +: 'NoneType' and 'int'" So what is actually going on behind the
> scenes?
>
> Ramit
>

+= is a single operator that is equivalent to typing b = b + 2

=+ is two operators put together:

b+=2 is equivalent to b += 2 is equivalent to b = b + 2

b=+2 is equivalent to b = +2 is equivalent to b = 2

b = + 2 is legal syntax, while b + = 2 is not.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110425/b6078e20/attachment.html>


More information about the Tutor mailing list