Very simple, but not working IF statement - help!


#1

New to Python (and any programming at all) as of this week.
Trying to create something simple, but failing.

def question():
    answer = str(raw_input("Would you like to know it's location? Y/N:")).upper
    if answer != "Y" and answer != "N":
        print "Invalid: You can only respond with Y or N"
    elif answer == "Y":

The elif statements following the initial if shown here are never evaluated, regardless of the input given.

i.e. if the input is y or n or Y or N, I want it to then consider the elif, but it always prints the message beneath the if statement.

This is the console output:

Would you like to know it's location? Y/N: Y
Invalid: You can only respond with Y or N

Can someone please give me a tip / advice on what I am doing wrong?

Thanks in advance.


#2

Personally I would just simplify your if condition to

ans = 'Y'
if ans not in ("y", "Y", "n", "N"):
    print("Failed")

or

ans = 'Y'
if ans not in "yYnN":
    print("Failed")

As a function

def question():
    ans = raw_input("Would you like to quit?")
    if ans not in "yYnN":
        return False
    return True

That should do you good.


#3

Hi zeziba,

Thanks for your help, much appreciated. I'll use this for now to make the function work.

I was wondering, do you know why what I've put there isn't working? I'd really like to understand that too so I can improve.

Many Thanks


#4

Hi.

I'm still struggling with this, and still don't understand why the code that I'm using doesn't work.

Can someone please advise?

It seems to go wrong in a different way each time I run it. The only bit working is the initial part of the if statement, but the elifs never quite deliver.

def question():
    answer = str(raw_input("Would you like to know the ship's location? Y/N:"))
    if answer not in ("Y", "N", "y", "n"):
        print "Invalid: You can only respond with Y or N"
        question()
    elif answer == "Y" or "y":
        print "The ship's location was: " + str(ship_row + 1) + ", " + str(ship_col + 1)
        board[ship_row][ship_col] = "!"
        print_board(board)
        print "GG."
    elif answer == "N" or "n":
        print "Fair enough...."

Below is one example of the console output, and one of the ways it tends to go wrong..

Would you like to know the ship's location? Y/N: k
Invalid: You can only respond with Y or N
Would you like to know the ship's location? Y/N: N
The ship's location was: 3, 5
O O O O O
O X O O O
O O X O !
O X O X O
O O O O X
GG.
None

In the example above my understanding is that I have managed to change the variable "answer" to equal "N", but then the elif for when "answer" is equal to "Y" has been run. I'm so confused :confused:

To clarify, I'm not really so much looking for ways to simplify the code I am using, but more to understand it better i.e. why isn't this working? What am I doing wrong / missing or mis-using?

Thanks in advance!


#5

Try changing elif answer == "Y" or "y": into elif answer == "Y" or answer == "y":.
This goes for the other elif aswell.


#6

After making this change everything works! Thanks a lot man. Really appreciated.

Out of interest, any thoughts on why my elif for "y" was being given when entering "n" before? I still don't understand that part.


#7

To be honest, I have no idea. :neutral_face:


#8

The problem is with the line:

elif answer == "Y" or "y":

Python interprets this as two options:
1. Does answer == "Y"? OR
2. Does "y" exist?

Remember that in boolean logic (AND/OR/NOT), an OR expression evaluates to True if any part is true. In the above expression, Python sees #2 as true regardless of whether the actual input was "Y", "y", "N", or "n", therefore the entire expression is True, so it proceeds with the Y/y elif block and ignores the N/y elif block.

The line should be something like this instead:
elif answer == "Y" or answer == "y":


#9

Masterful! Thanks for the lesson, I won't be making that mistake again.