Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if-else, using the True and False statements on python. skulpt.org

This is my code where I had to create it so it allows

  1. The user inputs the lengths of three sides of a triangle as Length1, Length2 and Length3
  2. If any two sides have the same length the program outputs "Isosceles"
  3. Otherwise the program outputs "Not isosceles"

However, the output part doesn't seem like working. I am new to this type of coding, so please help?

Length1 = raw_input()
Length2 = raw_input()
Length3 = raw_input()

print Length1
print Length2
print Length3
print Length1 == Length2
print Length2 == Length3
print Length1 == Length3

if Length1 == Length2 is True:
    print "Isosceles"
else: 
    print "Not Isosceles"  

if Length2 == Length3 is True:
    print "Isosceles"
else:
    print "Not Isosceles"

if Length1 == Length3 is True:
    print "Isosceles"
else:
    print "Not Isosceles"
like image 971
Dana Lanka Avatar asked Feb 06 '26 17:02

Dana Lanka


1 Answers

The problem is that Python interprets

if Length1 == Length2 is True:

like

if Length1 == Length2 and Length2 is True:

Comparison operators, like <, ==, or is are chained. This is very useful for, e.g. a < b < c, but it can also result in some unexpected behaviour, as in your case.

Change those checks to

if (Length1 == Length2) is True:

or better, just use

if Length1 == Length2:

Alternatively, you could just count the number of distinct sides using a set:

distinct_sides = len(set([Length1, Length2, Length3]))
if distinct_sides == 1:
    print "Equilateral"
if distinct_sides == 2:
    print "Isosceles"
if distinct_sides == 3:
    print "Scalene"  
like image 143
tobias_k Avatar answered Feb 09 '26 11:02

tobias_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!