Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this 'if' statement being read?

Tags:

python

In my code, I want the two answers 'Opposite' and 'Hypotenuse,' to have two separate outcomes, however, whenever I test the code and answer, 'opposite,' it ignores the rest of the code and goes down to the 'hypotenuse' questions. Have I formatted it wrong/is there a simpler way to do this/etc.?

from math import *

    def  main():

       #Trignometry Problem

        def answer():
            answer = raw_input()

    while True:

        # Phrase Variables
        phrase1 = ("To begin, we will solve a trigonometry problem using sin.")
        phrase2 = ("Which is known - hypotenuse or opposite?")
        phrase3 = ("Good! Now, we will begin to solve the problem!")
        phrase4 = ("Please press any key to restart the program.")

        print phrase1
        origin=input("What is the origin?")
        print phrase2
        answer = raw_input()
        if answer == ("Hypotenuse.") or ("Hypotenuse") or ("hypotenuse") or ("hyotenuse."):
            hypotenuse=input("What is the hypotenuse?")
            print "So, the problem is " + "sin" + str(origin) + " = " + "x" + "/" + str(hypotenuse) + "?"
            answer = raw_input()
            if answer == ("Yes.") or ("yes") or ("yes.") or ("Yes"):
                print phrase2
            answer = raw_input()
            print phrase4
            answer = raw_input()
            if answer == ("No."):
                break 
        if answer == ("Opposite."):
            opposite=input("What is the opposite?")
            print "So, the problem is " + "sin" + str(origin) +  " = " + str(opposite) + "/" + "x" + "?"
            answer = raw_input()
            if answer == ("Yes.") or ("yes") or ("yes.") or ("Yes"):
                print phrase2
        answer = raw_input()
        print phrase4
        answer = raw_input()
        if answer == ("No."):
            break


    main()
like image 223
krankes kind Avatar asked Dec 21 '22 21:12

krankes kind


1 Answers

Short answer

You probably want to change those:

if answer == ("Hypotenuse") or ("Hypotenuse.") ...

By this:

if answer in ("Hypotenuse", "Hypotenuse.", ...):

Explanation

The expression:

answer == ("Foo") or ("Bar")

It is evaluated like:

(answer == ("Foo")) or (("Bar"))

And "Bar" is always True.

Obviously, as pointed out in the comments, "HYPOTENUSE" in answer.upper() is the best solution.

like image 63
C2H5OH Avatar answered Jan 04 '23 13:01

C2H5OH