While you're at it



Ok, so I understand how to get through this lesson, however I do not understand what the purpose is for the = operater in the string num += 1

Why shouldn't num + 1 work?

Update: ok so what I don't understand is how the += operand works. Can anyone Eli5?

Also, I feel like calling num += 1 a "string" is incorrect, num is a variable but i don't know the correct terminology for num +=1
pls halp, thanks.

num = 0

while num <= 10:  # Fill in the condition
    num +=1
    print num ** 2
    # Print num squared
    # Increment num (make sure to do this!)


This is known as a compound operator, assignment over addition.

The equivalent would be,

num = num + 1

Using the compound operator, the addition takes place first, then the new sum is assigned back onto the variable.


Ok, thanks! I remember how the += operand works. But when i do that exercise with the + operand instead of the += operand, i get repeating 0's. Shouldn't it be repeating 1's since it's re-iterating through num + 1?

This question is a bit off topic but I'd appreciate it if you'd be able to help me understand the result of this code:

num = 0

while num <= 10: # Fill in the condition
num + 1
print num ** 2


That is only an expression, not a statement. It needs to be an assignment in order to be a statement.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.