Skip to content
Advertisement

Why doesn’t it keep incrementing every time I run the block?

I’m an absolute beginner, and I don’t understand why a is not incremented every time I run the code. It always prints a = 0 and a = 1.

a = 0
b = 2

print ("a = {}".format(a))

a = a + 1

print ("a = {}".format(a))

Output

I’m working on Google Colab. Thanks in advance!

EDIT: I just noticed if I keep the two first lines on a separate block like this, the code does increment. Why??

Advertisement

Answer

Use two separate cells, one for declaring the variables and one for incrementing:

colab-example

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement