dimanche 27 novembre 2016

why does the order of my if statement affect what gets printed? python [duplicate]

This question already has an answer here:

I am new to programming and i have come across this when i have been trying to make a simple game. You have to guess a letter and if it is in the word it should response with that letter is in the word and if it isn't then it should say it isn't. Instead is just prints the first if statement result no matter what the input is. i switched the if and elif order around and it still only printed the first if statement result. How do i fix this? Here is my code:

    response2 = input("What is your first letter or guess at the word?")
    b = str(response2)
    if b != "s" or "n" or "a" or "k" or "e":
        print("That letter is not present. You have 6 lives remaining.")
        lives -= 1
    elif b == "s" or "n" or "a" or "k" or "e":
        print("That letter is in the word!")
    elif b == "snake":
        print("You won! That was quick!") 

No matter what the input is here is prints "That letter is not present." swapping the order meant that what ever the input it said "That letter is present" even when it didn't fix the if statement.

Aucun commentaire:

Enregistrer un commentaire