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?")
answer = input()
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.
 
Ad

Advertisements

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?")
answer = input()
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:

if answer == "yes" or answer == "Yes" or answer == "y":

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 answer.lower().startswith("y"):

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

if answer.lower().startswith("ye"):
do_Yell_Action(rest_of_input_line)
elif answer.lower().startswith("yi"):
do_Yield_Action(rest_of_input_line)

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

answer = answer.lower()
if answer.startswith(...

{to expand (2.x syntax)}

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

(answer, args) = (command[0], command[1:])

operation = None
if answer.startswith("ye"):
operation = do_Yell_Action
elif answer.startswith("yi"):
operation = do_Yield_Action
elif answer.startswith("dr"):
operation = do_Drop_Action
....

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

Advertisements

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Another student in my class showed us this code 2
Please help with this 8
new to While statements 9
Please Help! 1
back with more issues 13
Can you please help me? 5
Please help if you can! 13
import serial failure 2

Top