Why doesn't my sum update?

x = 1
y = 2
sum = x + y

print (sum)# I get 3 as expected

y = 3
print (sum)# Still get 3 while expecting 4

1 Like

Sum is variable which is already assigned with the sum of x and , so if you change the value of y also , the value of sum will not change . if you really want to change the value of sum then you can use the below program.

x = 1
y = 2

sum = x+y
print sum
3

y = 3
sum = x+y
print sum
4

1 Like

Remember that computer code is not algebra. the = symbol is not an “equals sign,” it is an assignment operator: it assigns the value on the right to the variable on the left, no more.

To do what you are thinking - to make sum continually update - you will need a sum() function: next lesson, I believe.

1 Like