简体   繁体   中英

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. 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.

Just set x = False outside of the while loop and that'll solve it. Example:

x = False

while True:
    # do stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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