Issues with if and elif statements in 3.3

Dave Angel davea at davea.name
Thu Aug 8 06:34:57 EDT 2013


krismesenbrink at gmail.com wrote:

> 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"):




>         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.

-- 
DaveA





More information about the Python-list mailing list