lundi 18 mai 2015

if, elif and else (with 'or') [duplicate]

This question already has an answer here:

I have been trying to create a program which responds differently due to different user inputs. Here is an example of what i'm trying to do:

user = input(">>> ")
words = user.split()

if "one" or "a" in words:
    print("NO. 1")

if "two" or "b" in words:
    print("NO. 2")

if "three" or "c" in words:
    print("NO. 3")

else:
    print("nope.")

The problem with this code is that it executes all of the ifs (and not the else), no matter what you input. I have also tried the code with elifs instead, which only execute the top if no matter what the input.

I have also run the program without the ors and I was wondering if this might be why there is a problem?

I'd like to know if I am doing anything wrong and whether there is a way to fix this problem. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire