Cady
Cady

Reputation: 3

Python: Variables not changing

My variables are not changing when I am subtracting from them. I am supposed to have the xPos and yPos change and then print out the change.

It does not have any errors.

xPos = 400 
yPos = 400 
rain = False 
sidewalk = True 

print("Your robot is located at", xPos , "on the x-axis and", yPos , "on the y-axis")

yPos - 5
xPos-100

print("Your robot is located at", xPos , "on the x-axis and", yPos , "on the y-axis")

It prints "Your robot is located at 400 on the x-axis and 400 on the y-axis" twice instead of printing that once and "Your robot is located at 300 on the x-axis and 395 on the y-axis".

Upvotes: 0

Views: 121

Answers (2)

Steve
Steve

Reputation: 1943

You need to reallocate the subtraction to a variable. It can be the same variable if you want.

yPos = yPos - 5
xPos = xPos - 100

If you just have the lines you have the Python interpreter does the maths but has nowhere to store the result. So in the terminal you can do this:

y = 100
print(y) # prints 100
y - 5 # outputs 95
print(y) # prints 100

Upvotes: 1

Djaouad
Djaouad

Reputation: 22776

You are subtracting, but the results of the subtraction are not being used (because numbers are immutable, you can't change them in-place), you need to assign those results back to the variables you're subtracting from:

yPos = yPos - 5    #  or, yPos -= 5
xPos = xPos - 100  #  or, xPos -= 100

Upvotes: 2

Related Questions