0

I am brand new to programming. Is there any way to simplify the conditional operators

a = 50; b = 70; c = 60; 

# Classification 

if (a == b and b == c and c ==a):
   print('Equilateral triangle')

elif (a == b or b == c or c == a):
   print('Isosceles triangle')

elif (a!=b and b!=c and c!=a):
   print('Scalene triangle')
2
  • 1
    if a == b == c Commented Mar 1, 2019 at 11:45
  • 1
    As a side note, you might consider including inequalities for the case when a b and c do not make a triangle at all. Commented Mar 1, 2019 at 11:45

2 Answers 2

6

You can shorten the comparisons that use and by using chained comparisons, and by dropping one of the tests (they are mutually exclusive), using else instead:

if a == b == c:
   print('Equilateral triangle')

elif a != b != c != a:
   print('Scalene triangle')

else:
   print('Isosceles triangle')

Note that Python's if syntax doesn't require any parentheses around the test expressions.

Next, you could look at these values as a set, and test how many elements are in the set:

unique_lengths = len({a, b, c})

if unique_lengths == 1:
   print('Equilateral triangle')

elif unique_lengths == 2:
   print('Isosceles triangle')

else:
   print('Scalene triangle')

This can then be turned into list lookup, mapping 1, 2 and 3 to triangle class names; I slotted None into the 0 position:

classes = [None, 'Equilateral', 'Isosceles', 'Scalene']
print(classes[len({a, b, c})], 'triangle')
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your help. Out lecturer encourages us to use parentheses. We haven't learned about the set yet.
@BiLAL: Then your lecturer is going against the industry standard Python styleguide, but best stick to the conventions they set for now. :-) You'll come across sets soon enough, they can only contain unique elements (as tested by equality), and allow for fast membership testing (does this set contain this specific value?), among other handy operations. Turning a, b, c into a set when there are shared values among those 3 variables means you'll have a set with fewer than 3 values in it, and we can exploit that to count how many sides are equal.
Thank you Martijin. I went through the styleguide and it was very helpful. Also i have started learning about sets on youtube. :)
Is there any document where i can refer to and find a list of all the possible comparisons for different scenarios?
@BiLAL: not sure what you mean, but that sounds like a very broad question. The reference documentation for comparisons I already linked to, and the documentation for standard types cover any type specific operations.
0

Using a list:

lis = [a,b,c]
lis.sort()
if lis[0] == lis[-1]:
    print("Equilateral triangle")
elif lis[0] == lis[1] or lis[-1] == lis[1]:
    print("Isosceles triangle")
else:
    print("Scalene triangle")

2 Comments

Thank you for your help. But i have not learned about list yet.
That's ok . But after some months, you will find that codes from Martijn is so clear and ingenious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.