簡體   English   中英

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 等

由於某種原因,我的 output 是 300、300、300、300 等。

我該如何解決? (提前致謝): )

我想這段代碼在一個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

嘗試將其更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM