Why does the if and elif function not work properly in my code

Question:

I am very much a beginner in code. I have made this code (this is an abridged version) for my homework assignment but whenever I press play and start the process, no matter what number of sides I input, it always asks for a number of parallel sides and then guesses and isosceles triangle. Why is this the case?

print("Shape Guesser Game n-----------------")
print("Welcome to the Shape Guesser Game! nI believe that I can guess the shape you think of.")
print("There is only 1 rule: nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("nDo you want to play? nPick Yes or No")
if response_1 == "Yes" or "yes":
    print("Great! Let's get started! n------------------")
    response_2 = input("Have you thought of your shape? Yes/No")
    if response_2 == "Yes" or "yes":
        response_3 = input("How many sides does it have?")
        if response_3 == "3" or "three":
            response_4 = input("How many equal sides does it have?")
            if response_4 == "2" or "two":
                response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
                if response_5 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

            elif response_4 == "3" or "three":
                response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
                print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

            else:
                print("Not valid:Error n-------------")

        elif response_3 == "4" or "four":
            response_7 = input("How many parallel sides does it have?")
            if response_7 == "two" or "2":
                response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
                if response_8 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")
            if response_7 == "four" or "4":
                response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
                if response_9 == "yes" or "Yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

    elif response_2 == "No" or "no":
        print("Okay no worries, I will give you more time!")

elif response_1 == "No" or "no":
    print("Sad to see you go! Back to Home.")
Asked By: Juihi Charadva

||

Answers:

Use this:

print("Shape Guesser Game n-----------------")
print("Welcome to the Shape Guesser Game! nI believe that I can guess the shape you think of.")
print("There is only 1 rule: nYou can only think of a polygon with a maximum of 6 sides.")
response_1 = input("nDo you want to play? nPick Yes or No")
if response_1.lower() == "yes":
    print("Great! Let's get started! n------------------")
    response_2 = input("Have you thought of your shape? Yes/No")
    if response_2.lower() == "yes":
        response_3 = input("How many sides does it have?")
        if response_3 == "3" or response_3 == "three":
            response_4 = input("How many equal sides does it have?")
            if response_4 == "2" or response_4 == "two":
                response_5 = input("Aha! It is an isosceles triangle! Am I right? Yes or No?")
                if response_5.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

            elif response_4 == "3" or response_4 == "three":
                response_6 = input("Aha! It is an equilateral triangle! Am I right? Yes or No?")
                print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

            else:
                print("Not valid:Error n-------------")

        elif response_3 == "4" or response_3 == "four":
            response_7 = input("How many parallel sides does it have?")
            if response_7 == "two" or response_7 == "2":
                response_8 = input("Aha! It is a trapezium! Am I right? Yes or No?")
                if response_8.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")
            if response_7 == "four" or response_7 == "4":
                response_9 = input("Aha! It is a parallelogram! Am I right? Yes or No?")
                if response_9.lower() == "yes":
                    print("Yay I have succeeded! Thank you for playing this game. nGo back to Home.")

    elif response_2.lower() == "no":
        print("Okay no worries, I will give you more time!")

elif response_1.lower() == "no":
    print("Sad to see you go! Back to Home.")

or doesn’t work just like if a == 'b' or 'c':, you have to make it if a == 'b' or a == 'c':

Also stuff like if response_1 == "Yes" or "yes" that can just become if response_1.lower() == "yes", so YeS and yEs would be considered 'yes'.

Answered By: U13-Forward

You actually have the same issue in each of your if statements. When you write something like

if response_1 == "Yes" or "yes"

python interprets this as

if (response_1 == "Yes") or ("yes")

That is: if the variable response_1 is equal to “Yes”, or if the string “yes” is truthy, then the if statement runs. “yes” is truthy, so the if statement always runs. (See here for example for more information about what truthiness means).

What you probably wanted was to check if the variable response_1 is equal to “Yes” or if it’s equal to “yes”, which is written as:

if response_1 == "Yes" or response_1 == "yes"`

And of course you’ll need to apply this same correction to the rest of your if statements as well.

Answered By: Flight Odyssey
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.