I’ve tried this, and I’d like to know more about why this doesn’t work. count += 1
works as intended, but count =+ 1
does not.
list = ["one", "two", "three"]
count = 0
for x in list:
count =+ 1
print(x)
print(count)
The above example doesn’t work properly. However, the one below does.
list = ["one", "two", "three"]
count = 0
for x in list:
count += 1
print(x)
print(count)
The intended output is to have it print each iteration all the way up to the number 3.
>Solution :
Because that’s not proper syntax. The operator is +=
("plus equals"), not =+
("equals plus").
count =+ 1
is the same thing as count = (+1)
, where +
is a unary operator on 1
.