Python 为真时将值增加 100
Python increment value by 100 while true
if 150 <= center_x <= 180:
x = 200
x += 100
MESSAGE = str(x)
我是运行这个说法。虽然是真的,但我希望 x 增加 100,从而输出:300、400、500、600 700 等
出于某种原因,我的输出是 300、300、300、300 等。
我该如何解决这个问题? (提前致谢):)
尝试将其更改为:
x = 200
if 150 <= center_x <= 180:
x += 100
MESSAGE = str(x)
我想这段代码在 while True 循环中。如果是这样,那么问题是您在每次迭代时将 x 设置为 200,然后将其递增 100,每次迭代给出 300。您应该将起始值提供给循环外的 x。
#First you need to define your center_x. You may get this value from some other
#function in your script. I will use 160 as a valid example
center_x = 160
#You need to define initial value of x outside of the loop so it does not "reset"
x = 200
if 150 <= center_x <= 180:
while x <= 600: #Here you set the limit of where you want to stop adding. I used 600 as example
x += 100
print(x) #There is no need to set a MESSAGE variable, you can directly print the x variable
if 150 <= center_x <= 180:
x = 200
x += 100
MESSAGE = str(x)
我是运行这个说法。虽然是真的,但我希望 x 增加 100,从而输出:300、400、500、600 700 等
出于某种原因,我的输出是 300、300、300、300 等。
我该如何解决这个问题? (提前致谢):)
尝试将其更改为:
x = 200
if 150 <= center_x <= 180:
x += 100
MESSAGE = str(x)
我想这段代码在 while True 循环中。如果是这样,那么问题是您在每次迭代时将 x 设置为 200,然后将其递增 100,每次迭代给出 300。您应该将起始值提供给循环外的 x。
#First you need to define your center_x. You may get this value from some other
#function in your script. I will use 160 as a valid example
center_x = 160
#You need to define initial value of x outside of the loop so it does not "reset"
x = 200
if 150 <= center_x <= 180:
while x <= 600: #Here you set the limit of where you want to stop adding. I used 600 as example
x += 100
print(x) #There is no need to set a MESSAGE variable, you can directly print the x variable