2

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"
Isaac G Sivaa
  • 1,289
  • 4
  • 15
  • 32
Dana Lanka
  • 23
  • 2

3 Answers3

9

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"  
Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179
2

I would just make a function that checks each pair to see if they are equal

def isosceles(x1, x2, x3):
    return x1 == x2 or x2 == x3 or x1 == x3

# slightly faster version since it returns after the first True
def isosceles(x1, x2, x3):
    return any(x1 == x2, x2 == x3, x1 == x3)

Make sure you convert the inputs to int so you aren't comparing string values

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

Then call your function

if isosceles(Length1, Length2, Length3):
    print "Isosceles"
else:
    print "Not isosceles"
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1
if Length1 == Length2: # don't use `if Length1 == Length2 is True:`
    print "Isosceles"

elif Length2 == Length3:
    print "Isosceles"

elif Length1 == Length3:
    print "Isosceles"
else:
    print "Not Isosceles"

You need to use elif, elif's are only evaluated if the previous statement is False, if's are always evaluated.

if Length1 == Length2 is True: is not the same as if Length1 == Length2:

Your statements would only be True if both Length1 and Length2 were = True.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • "Unless Length2 is actually False your statements will always evaluate to True" Maybe I misunderstood you, but I think this is not correct. Unless `Length2` is `True`, it will always return false. E.g., `3 == 3 is True` -> `False` – tobias_k Aug 14 '14 at 17:43
  • @tobias_k, yes should have put True – Padraic Cunningham Aug 14 '14 at 17:51