# Issues with if and elif statements in 3.3

K

#### krismesenbrink

def town():
print ("You stand in the middle of Coffeington while you descide what"
" to do next, you have herd rumor of the Coffeington Caves that run"
"under the city, would you like to check them out?")
if answer == ("yes") or ("Yes") or ("y"):
print("You set out for the Coffeington Caves")
elif answer == ("no") or ("No") or ("n"):
print("Oh...well im sure you can find something else to do")
else:
print("You just stand there")
town()

i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway.

D

#### Dave Angel

def town():
print ("You stand in the middle of Coffeington while you descide what"
" to do next, you have herd rumor of the Coffeington Caves that run"
"under the city, would you like to check them out?")
if answer == ("yes") or ("Yes") or ("y"):

This doesn't do what you think it does. First it compares answer to
"yes". Then it takes the result of that and OR's it with "Yes". Then
it takes the result of that and OR's it with "y". Finally it takes the
bool of the result and decides whether to execute the if-body. Since
those OR's will always be true, it always executes the if-body, and
never the elif or else body.

Fix the expression to what you presumably meant:

Or less typing:

if answer in ("yes", "Yes", "y"):

K

#### Kris Mesenbrink

WOW as if it was something as easy as that,i had been looking for awhile on what i was doing wrong. as it seems i just don't know my way around if statements at all, thank a bunch for this. makes everything else i have been code work

thanks again

D

#### Dennis Lee Bieber

Or less typing:

if answer in ("yes", "Yes", "y"):

Or (IF there is no chance for ambiguity)

If there is ambiguity, provide enough of the matchword to determine the
unique option...

do_Yell_Action(rest_of_input_line)
do_Yield_Action(rest_of_input_line)

Granted, if parsing command options, you might want to lift the
..lower() out of the "if" conditions

{to expand (2.x syntax)}

command = raw_input("Enter command> ")
command = command.lower().split()

operation = None
operation = do_Yell_Action
operation = do_Yield_Action
operation = do_Drop_Action
....

if operation is not None:
operation(args)
else:
print "That is not a valid command"

D

#### Dennis Lee Bieber

WOW as if it was something as easy as that,i had been looking for awhile on what i was doing wrong. as it seems i just don't know my way around if statements at all, thank a bunch for this. makes everything else i have been code work

Actually, it is /not/ the if you misunderstood -- but the precedence of
Boolean algebra (the conditional can be lifted out of the if and put on a
separate line):

cond = something == x or y or z

if cond:

The flaw would be in the first statement, not the IF.

Python does support shortcut form of:

cond = value <= x <= other

(note: you can use other comparisons, but know what you are doing)

That expands to the equivalent of

cond = value <= x and x <= other