Python将值增加100,而为true

2024-03-29 02:20:56 发布

您现在位置:Python中文网/ 问答频道 /正文

 if 150 <= center_x <= 180:
            x = 200
            x += 100
            MESSAGE = str(x)

我在做这个声明。当为true时,我希望x增加100,从而输出:300400500600700,等等

由于某种原因,我的输出是300300300等等

我该如何解决这个问题?(提前感谢):)


Tags: true声明messageifcenterstr
3条回答

我假设这段代码在而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

尝试将其更改为以下内容:

x = 200
if 150 <= center_x <= 180:
    x += 100
    MESSAGE = str(x)

相关问题 更多 >