当我从变量中减去变量时,变量并没有变化。我应该有xPos和yPos的变化,然后打印出来的变化。
它没有任何错误。
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")它打印两次"Your robot is located at 400 on the x-axis and 400 on the y-axis",而不是打印一次和"Your robot is located at 300 on the x-axis and 395 on the y-axis"。
发布于 2019-09-01 22:35:06
您正在减除,但是没有使用减法的结果(因为数字是不可变的,您不能就地更改它们),您需要将这些结果赋值给您要减去的变量:
yPos = yPos - 5 # or, yPos -= 5
xPos = xPos - 100 # or, xPos -= 100发布于 2019-09-01 22:36:21
您需要将减法重新分配给一个变量。如果你想的话,它可以是相同的变量。
yPos = yPos - 5
xPos = xPos - 100如果您只有一行代码,那么Python解释器可以做数学运算,但是没有地方存储结果。所以在终端中你可以这样做:
y = 100
print(y) # prints 100
y - 5 # outputs 95
print(y) # prints 100https://stackoverflow.com/questions/57749917
复制相似问题