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.
Related
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 :)
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.
Simple question here, made a little program asks the user to create a username and password. Every thing worked until I added this "if user" statement. It should restart the loop if I enter 'x' or 'X' but it restarts it regardless of what I enter. What's happening here?
db = {}
num_of_entries = 0
def addToDict(a, b):
db[a] = b
print(f"User created: '{a}'")
def removeFromDict(key):
del db[key]
print()
print(f"User '{key}' has been removed.")
while True:
clear = "\n" * 100
print()
print("""
Hi there!
Would you like to create a new account or delete an existing one?
('Create' to create, 'Delete' to delete)
""")
choice = input("> ").upper()
if choice == 'CREATE':
print(f'{choice} mode selected.')
print()
user = input("Please enter a username: ")
if user == 'X' or 'x':
continue
else:
if user not in db:
passW = input("Please enter a password: ")
print(clear)
print()
addToDict(user, passW)
The problem here is that you are posing two separate conditions user == 'X' and 'x'.
since 'x' is not null or false it will always be true making the if statement always true as it uses an or operator between the two conditions.
What you need to do as is suggested above is:
if user == 'X' or user == 'x':
You could also do:
if user.lower() == 'x':
Or even:
if user in ['X', 'x']:
I'm making a guessing game or computer science in school where the number to guess is seven. I have tried using while loops and if elif else statements but it doesn't seem to want to make a conditional loop My code is as follows:
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
else:
print("Well done")
Any help would be appreciated thanks. I need it in about a week and a half's time.
If you're trying to allow your player to continuously guess the input needs to be at the top of the while loop, before the conditional-branch
while(True):
guess = input("Make a guess: ")
if(guess == 7):
print(guess,"was correct!")
break
else:
print("Nope. Guess again.")
Of course, you could make it more interesting in a variety of ways.
guess=int(input("Guess a number!"))
var=1
while var==1:
if guess !=7:
print("Try again")
guess=int(input("Guess a number!"))
else:
print("Well done")
var=0 #set var to 0, to exit the loop
Try this. You need to exit the loop, and to do that, var needs to be set to 0.
I am new to python I am trying to code this, I am asking a question, hence the "Are you a mutant" and depending on if the user responds with a yes or no it should come up the respective output but it works only for yes but not for no. how do i make it work for the elif output?
print("Are you a mutant?")
answer = input()
if 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif 'No':
print("Your application was not successful on this occassion")
`
You need to compare the variable that stores the users input with the thing you are comparing it to. In this case using the ==. Below is revised code off your example:
print("Are you a mutant?")
answer = input()
if answer == 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == 'No':
print("Your application was not successful on this occassion")
You have to write raw_input instead of input. 'Input' just takes the text value but 'raw_input'get the input as a string.
If you are using python2, then follow the code below:
print("Are you a mutant?")
answer = raw_input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")
In python3 raw_input() was renamed to input(). Then follow the code below:
print("Are you a mutant?")
answer = input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")