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

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

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.

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.

Is C similar to C# (C-sharp); or are they completely different ? I was wondering if I should begin learning C# and the transition to C will be a tad ...

Write a program that displays all numbers divisible by 3 and 4 within a range entered by user. Display five numbers per line. Numbers are separated by exactly two spaces. ...

Hi guys. I am trying to create a program where the user inputs an integer and the program will cout the nth prime number. Unfortunatly when I try to cout ...