the objective is to create a function to determine, from the three sides of a triangle, whether it is isosceles or not. I wrote the following code, yet its not very efficient, how could i have done this better? and is there anything wrong with this code? i tested it and it passed, but its a bit overly complicated for the task.

def isIsosceles(x, y, z):
	l = [x, y, z]
	for a in l:
		for b in l:

			if a <= 0:
				return False
			elif a == b:
				if x == y or x == z:
					return True
			else:
				return False

Recommended Answers

All 10 Replies

def isIsosceles(x, y, z):
   return x == y or y == z
def isIsosceles(x, y, z):
   return x == y or y == z

hahaha, i just realized that, i learnt some bad habits somewhere. thank you!

def isIsosceles(x, y, z):
   return x == y or y == z

whilst this thread is open, could you help me with another problem?

this time i am to determine whether its scalene or not, so i wrote;

def isScalene(x, y, z):
	if x <= 0 or y <=0 or z <=0:
		     return False
	elif x == y or x == z:
		  return False
	else:
		return True

but its not being accepted. whats wrong with this?

whilst this thread is open, could you help me with another problem?

this time i am to determine whether its scalene or not, so i wrote;

def isScalene(x, y, z):
	if x <= 0 or y <=0 or z <=0:
		     return False
	elif x == y or x == z:
		  return False
	else:
		return True

but its not being accepted. whats wrong with this?

I understand that a trinagle is scalene if it is not isoceles, so this should work

def isScalene(x, y, z):
    return not isIsoceles(x, y, z)

You issue may come from the tab characters that you're using in your code. Configure your editor to indent python code with 4 spaces. Furthermore, pyTony's function doesn't work, one could have x == z when his function returns False. I suggest

def checkValidTriangle(x, y, z):
    if x < 0 or y < 0 or z < 0:
        raise ValueError("Negative side in triangle.")
    elif not (abs(x-y) <= z <= x + y):
        raise ValueError("Impossible triangle.")

def isIsoceles(x, y, z):
    checkValidTriangle(x, y, z)
    return (x == y) or (x == z) or (y == z)

# or

def isIsoceles(x, y, z):
    checkValidTriangle(x, y, z)
    return len(set((x, y, z))) <= 2

def isScalene(x,y,z):
if x == y or x == z or y == z:
return False
elif x <= 0 or y <=0 or z <= 0:
return False
else:
return True

def isScalene(x,y,z):
if x == y or x == z or y == z:
return False
elif x <= 0 or y <=0 or z <= 0:
return False
else:
return True

It works, only it's better to raise an exception if one of x, y, z is < 0, because there is no triangle with x = -1 for example. Actually, the function

def isValidTriangle(x, y, z)
    return abs(x-y) <= z <= x + y

suffices, because if this it returns True, then z >= 0 and also x - y <= x + y, so that y >=0 and y - x <= x + y, so that x >=0. You can use strict inequalities if you don't want 'flat' triangles.

It works, only it's better to raise an exception if one of x, y, z is < 0, because there is no triangle with x = -1 for example. Actually, the function

def isValidTriangle(x, y, z)
    return abs(x-y) <= z <= x + y

suffices, because if this it returns True, then z >= 0 and also x - y <= x + y, so that y >=0 and y - x <= x + y, so that x >=0. You can use strict inequalities if you don't want 'flat' triangles.

noted, thank you for the help.

if one of x, y, z is < 0

This is also expressible more obviously as:

def is_valid_triangle(*sides):
    return len(sides) == 3 and min(sides) > 0

Also your function return False for 39, 4, 874 and 14, 64, 82 and as you are missing also : at end of def line, I assume you had not chance to test the function.

39, 4, 874 and 14, 64, 82 are not valid triangles ! Try to draw them ! There is another way to write the function

def isValidTriangle(x, y, z):
    return min(x+y-z, y+z-x, z+x-y) >= 0

Ever heard of the triangle inequality ?

Ok, so you more far than OP, I do not think the mathematical validity was required for OP.

Triangle inequality with positiveness I understand more easily in form:

def is_valid_triangle(x,y,z):
    return x + y >= z > 0 and y + z >= x > 0 and x + z > y

I assume that you mean by flat triangle with 'triangle' with one or more sides 0. This function does not allow them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.