How to change a boolean from true to false using an input - python-3.x

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!")```

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 ; ))

I think what you're trying to accomplish is this:
door_is_closed = True
while 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

If statement issue with basic python program [python]

I a new in programming , was trying some concepts with if else in python
The if else statement is not working as it should.
I'm using nested if-else , however only included the basic code in the code block
I am using a string as an input and then comparing the input with if else statements.
I tried the code in Thonny ide and it works when I debug the program , but after trying to run the program it does not print anything . Alternatively if I use an else statement instead of the elif in the end , only the code in the else statement will print
the code is :
new_value = input("enter your choice between left and right")
if new_value =='left':
print("You chose left")
elif new_value =="right":
print("you chose right")
This code is correct.
new_value = input("enter your choice between left and right")
if new_value =="left":
print("You chose left")
elif new_value =="right":
print("you chose right")
Alternatively if you use an else statement reffer it,
if new_value =='left':
print("You chose left")
else:
print("you chose right")
provide your full nested loop so i will understand problem.

Strings not selected properly in if comparison

I have the following code.
while True:
# Prompt
command = input("> ").upper()
if command == "WEST" or "IN":
if adventure.move(command) == True:
print("True")
else:
print("You cannot go there")
elif command == "QUIT":
print("Thanks for playing!")
exit()
else:
print("Invalid command")
The idea is to prompt the user for a command. If the command is either direction "WEST" or "IN" its supposed to move and give a description. This all works. The idea is that an adventure consists of several rooms a user must navigate through
For the record: adventure.move(command) returns True if the move was succesful, and False if the move could not be made. Because there was no direction to be going in, for example.
The problem is that if I give a command like QUIT or FOO I am expecting a different result. However, this does not happen.
>WEST
True (move successful)
>QUIT
You cannot go there
>FOO
You cannot go there
It seems that whatever I type; it will always accept the first if statement.
Any clue what I am doing wrong?
You need to change your first if statement to this
while True:
# Prompt
command = input("> ").upper()
if command == "WEST" or command == "IN":
if adventure.move(command) == True:
print("True")
else:
print("You cannot go there")
elif command == "QUIT":
print("Thanks for playing!")
exit()
else:
print("Invalid command")
if command == "WEST" or "IN": will always evalutate to true because it is actually asking if command is equal to west or the equivalent of bool("IN") which it will always return true unless it is an empty string. And in an or statement if either of the logic tests return true then the code will be executed in the if block.

how do I break while loop by raw_input()? python 3

I need some help understanding the differences between the following. In the first example, I want the loop to break when the user inputs False:
true = True
while true:
print("Not broken")
true = input("to break loop enter 'False' ")
There was a question asked at:
how do I break infinite while loop with user input
Which gives this solution:
true= True
while true:
print("Not broken")
true = input("to break loop enter 'n' ")
if true == "n":
break
else:
continue
And I don't understand why the first method doesn't work and the second does??? Why doesn't python take the input as if someone was changing the script and change the variable "true"? Whats going on behind the scenes?
Any help would be appreciated. Thanks in advance :)
The while statement is conditional, and the user entering the String "False" will still resolve to a True outcome.
For an idea of what Python considers True and False, checkout this link: https://realpython.com/python-conditional-statements/
Building on this answer Converting from a string to boolean in Python?, the best way to check is:
true = True
while true is not 'False':
print("Not broken")
true = input("to break loop enter 'False' ")

Python Beginner: How can I change the definition of a previously defined variable?

Brand new to programming so please forgive my ignorance.
I want the script to begin with POWER = False, then trigger an event that makes it True for the rest of the script. The way I have it here doesn't save the POWER variable.
power = False
scuba = False
def computer_room():
if not power:
print("yadayadayada.")
time.sleep(1)
terminal_out()
elif power:
print("yadayadayada.")
time.sleep(1)
terminal_in()
def terminal_out():
print("yadayadayada.")
choice = input("> ")
if "password" in choice.lower():
print("What is the password?")
choice2 = input("> ")
if choice2 == "f1xh3rup":
power = True
terminal_in()
Define power as global before you use it in function
global power

Scopes within while loops

from random import randint
isRunning =True
while isRunning:
dice1 = randint(1,7)
dice2 = randint(1,7)
print("The first die landed on ℅d and the second landed on ℅d." ℅ (dice1,dice2))
user_input = input("Contiue? Yes or No?\n>")
if user_input == "Yes" or "yes":
print("="*16)
elif user_input == "No" or "no":
isRunning = False
I feel like I'm making such a simple mistake and when I decided to look into global variables and what not it still doesn't help. Could anyone explain why the while loop doesn't terminate, although the variable was set to false.
if user_input== "Yes" or "yes" should be
if user_input== "Yes" or user_input =="yes", alternatively it's equivalent to if any(user_input==keyword for keyword in["Yes", "yes"]):
your original if clause is splitting to
if user_input=="Yes" or if "yes" and if "yes" always true, therefore your if-elseif clause always go to if clause.

Resources