Python ELIF Statement Exercise

Exercises 2 Questions
ELIF-exercise

Python ELIF Statement Exercise

2 Questions

ELIF Statement Exercise:

Given the 3 sides of a triangle – x, y and z – determine whether the triangle is equilateral, isosceles or obtuse.

Note: Equilateral means all sides are equal, isosceles means two of the sides are equal but not the third one, obtuse means all 3 are different.

(Bonus question: Now include a check at the beginning to see whether the lengths of the sides satisfy the triangle inequality. The inequality states that every side of the triangle is shorter than the sum of the remaining two.)

The tutorial you may need: What is Elif in Python 

python-solution-image

This one sounds difficult but introducing context to a problem makes it much easier to imagine and solve. So, let’s get to it!

Apart from IF and ELSE statements, we will use the ELIF statement (known as ELSE IF in other programming languages). Furthermore, using ELIF statements allow us to easily separate more than two non-overlapping conditions.

Okay! The way we are going to effectively implement this is by clearly identifying which conditions need to be satisfied for one type of triangle, then use and ELIF to check for a second type, thus leaving ELSE to account for the last type.

Let us start by checking if the triangle is equilateral. Since equality is transitive, as long as x equals y and x equals z, then z equals y and the triangle is equilateral. Therefore, our first IF statement would only check two conditions and look as follows:

if x==y and x==z:

     print ("Equilateral")

Now, using an ELIF statement afterwards means we only have to deal with obtuse or isosceles triangles. This is because the computer checks the conditional statements chronologically. Not satisfying the initial IF statements guarantees the triangle is not equilateral when it reaches the ELIF statement.

Thus, if any two sides are equal we are dealing with an isosceles triangle. Therefore, the condition of the ELIF statement simply needs to check if one among x equals y, y equals z or z equals x is true. This would look as follows:

elif x==y or x==z or y==z:

     print ("Isosceles")

This means that any triangles which did not satisfy either condition will have to be obtuse. Therefore, we add the ELSE statement and the print function that follows it.

else:

    print("Obtuse")

Putting it all together, the entire code would look like this:

if x==y and x==z:

    print ("Equilateral")

elif x==y or x==z or y==z:

    print ("Isosceles")

else:

    print("Obtuse")

And that is all there is to it. Bear in mind, that we could have chosen the IF and ELIF statements to account for types of triangles other than the ones we did in this approach and the outcome would have been identical. Keep in mind that if we decide to test for isosceles triangles first, we need to explicitly state the third side being of different length.

 

Now for the bonus part of this exercise.

We would need to test if the triangle inequality holds true first, so we include an IF statement before our initial IF statement. For the sake of accuracy, we should even change the equilateral testing IF statement to an ELIF. Doing so means that the computer would check for the various types of triangles only if the triangle inequality does not hold true.

Therefore, we need an IF statement to check if the inequality is violated. For this to happen, one of the following 3 conditions needs to be satisfied. Either x is greater than the sum of y and z, y is greater than the sum of x and z or z is greater than the sum of x and y.

Therefore, our IF statement would be as follows:

if x>=y+z or y>= x+z or z>= x+y:

    print ("The triangle does not exist")

The way we have set this up means that if a triangle does not exist (does not satisfy the inequality), then we would not even bother checking what type it is.

Remember that we changed the equilateral IF statement to and ELIF statement, so the entirety of the new code would look like this:

if x>=y+z or y>= x+z or z>= x+y:

    print("The triangle does not exist")

elif x==y and x==z:

    print ("Equilateral")

elif x==y or x==z or y==z:

    print ("Isosceles")

else:

    print("Obtuse")

Now, I know some of this sounds very complex, but once you break it down into smaller increments the task is not too difficult to complete.

Keep up the good work! And if you want to try integrating an IF into a function… then click here.

Leave a Reply

Your email address will not be published.

A Free Data Science Career Guide?

Check your email shortly!