简体   繁体   中英

How would I make this else statement work

My else statement wont print for this program and I'm not sure why.

side1 = int(input("Enter one side of a triangle : "))
side2 = int(input("Enter one side of a triangle : "))
side3 = int(input("Enter one side of a triangle : "))

if side1 != side2:
    if side2 != side3:
        if side3 != side1:
            print("This triangle is scalene")
elif side1 == side2:
        if side2 == side3:
            if side3 == side1:
                print("This triangle is equilateral")
else:
    print("This triangle is isosceles")

Your logic is faulty: there is no conventional way to reach the else . Look at your preceding conditions:

if side1 != side2:
    ...
elif side1 == side 2:
    ...

That covers all typical situations; any numbers you enter will fall into one of those two clauses; no further conditions of your if-elif-else will matter.

I'm not going to hand you a whole solution; you still need to work through several layers of logic flaw.

The problem is that your if and elif statements already cover all possible cases as side1 can either be equal or not equal to side2. A way to fix this is to combine all your if statements like so:

side1 = int(input("Enter one side of a triangle : "))
side2 = int(input("Enter one side of a triangle : "))
side3 = int(input("Enter one side of a triangle : "))

if side1 != side2 and side2 != side3 and side3 != side1:
    print("This triangle is scalene")
elif side1 == side2 and side2 == side3 and side3 == side1:
    print("This triangle is equilateral")
else:
    print("This triangle is isosceles")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM