Home > Mobile >  How to change a boolean from true to false using an input
How to change a boolean from true to false using an input

Time:09-30

I'm trying to make this very simple door script to gain some experience,but i have no clue on how to make the "doorisclosed" variable change to false when the door is opened, i have tried to make an if statement saying that when input is O doorisclosed = false but that dosen't seem to help i have tried to look for the answer in here and other sites with no success,thanks in advance for the help.

while True:
    doorisclosed=true
    user_input=input("press O to open")
#what i tried to do:
If user_input == "O": doorisclosed= false
    if user_input == "O" and doorisclosed == True: print ("the door has been opened!")
    if user_input == "O" and doorisclosed == False : print ("you cant open an open door you donkey!")```

CodePudding user response:

while True:
    doorisclosed=True
    user_input=input("press O to open: ")
    if user_input == "O": doorisclosed = False
    if user_input == "O" and doorisclosed == True: print ("the door has been opened!")
    if user_input== "O" and doorisclosed == False : print ("you cant open an open door you donkey!")

After clean up few syntax errors, the code now run. But there is "dead logic" in the code. If you trace it carefully, you will find the condition of 2nd if never satisfied, thus you will never see "the door has been opened!".

Try again to modify your conditions to see if you can get it closer to what you want ; ))

CodePudding user response:

I think what you're trying to accomplish is this:

while True:
    door_is_closed = True
    user_input = input("press O to open: ")
    if user_input == "O" and door_is_closed:
       door_is_closed = False
       print("The door has been opened!")
    elif user_input == "O":
       print("You can't open an open door")

Put this code into some script and see if it does what you want, then try to change some parts and play with it, that way you'll learn the best :)

  • Related