简体   繁体   English

Python3布尔if语句

[英]Python3 boolean if statement

while True:

   x = False

   if x == False:
     if (float) <= (price):
        if not safe_mode:
            (Some function)
     x = True
     print(something)

   elif x == True:
     if (float) >= (price):
        if not safe_mode:
            (Some Function)
     x = False
     print(something)

that is my code, and i want that to loop, but what i get is 'x' doesn't want to change value to 'True'... and 'x = True' get grayed out. 那是我的代码,我想让它循环,但是我得到的是'x'不想将值更改为'True'...而'x = True'变灰。 I don't know why it doesn't want to work, i need all your help pls. 我不知道为什么它不起作用,我需要您的所有帮助。 i kinda stress finding the problem :( 我有点强调发现问题:(

The problem is that you set x back to False at the beginning of each iteration of the while loop, so its value is changed within each conditional block, but then changed back to False right after when the loop starts over. 问题在于,您在while循环的每次迭代开始时将x设置为False ,因此在每个条件块内将其值更改,但是在循环重新开始后立即将其更改为False

Just set x = False outside of the while loop and that'll solve it. 只需在while循环外设置x = False即可解决。 Example: 例:

x = False

while True:
    # do stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM