0

So I'm trying to code the hypotenuse theorem into python and for some reason even if I type the letter k when asking for the letter h, it continues as if I had entered h, even though it should take me to the else saying to try again.

print ("H for hypotenuse")
typ = input(":")
if typ == "H" or "h" or "Hypotenuse" or "hypotenuse":
    print ("What Side are you missing?")
    print("A for The leg, B for the base, C for The longest side")
    side=(input(":"))
    if side == "A" or "B" or "a" or "b":
        if side == "A" or "a":
            print("What is B?")
            b = int(input(":"))
            print("What is C?")
            c = int(input(":"))
            a = int((c*c)-(b*b))
            a1 = (a/a)
            print("A is ", a1)
        else:
            print("What is A?")
            a = input(":")
            print("What is C?")
            c = input(":")
            b = (c*c)-(a*a)
            b1 = b/b
            print("B is ", b1)

    else:
        print("What is A?")
        a = input(":")
        print("What is B?")
        b = input(":")
        c = (a*a)+(b*b)
        c1 = c/c
        print("C is ", c1)
else:
    print ("Please try again")
Mr Trash
  • 3
  • 1
  • there's something wrong with your if logic, I think you should have written it like this. if side == 'A' or side == 'B' or side == 'a' or side == 'b', and so on – Syahnur Nizam Jan 10 '20 at 01:36
  • Because of operator precedence you have `if (side == "A") or "a":` Since `"a"` will evaluate to not false, you have `if (side == "A") or True:` Any `or` statement with at least one `True` evaluates to True. So your whole statement can be re-written as `if True:`. – 001 Jan 10 '20 at 01:41

1 Answers1

1

if typ == "H" or "h" or "Hypotenuse" or "hypotenuse", if side == "A" or "B" or "a" or "b", and if side == "A" or "a" don't mean what you think they mean. Instead, you want to do if typ in {"H", "h", "Hypotenuse", "hypotenuse"}, if side in {"A", "B", "a", "b"}, and if side in {"A", "a"}.

Side note: you can simplify your code by using elif instead of nesting a duplicate check for "A" and "a".