0

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".

2
  • 1
    Try yPos -= 5! Commented Sep 1, 2019 at 22:35
  • You have to assign the result of the operation to something. Commented Sep 2, 2019 at 2:35

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.