This is me with another project for beginners :)
The Project as stated by ZZucker about 2 months ago:
"Given the length of three sides, write a function that checks if it is
possible to create a triangle."
I could solve it with an annonymus function like this:
is_triangle = lambda a,b,c: a+b!=c and a+c!=b and b+c!=a and c-b<a<c+b
But as I'm trying to understand using "class"es, I did code the following
class.
Now, Is this an acceptable class?
Thank you in advance for your comments :)
#from math import*
class Triangle(object):
"""Checks if given sides can make a Triangle then shows the Angles."""
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
self.A = self.B = self.C = 0
def _istriangle(self):
"""Checks if given sides can make a Triangle"""
if self.a+self.b!=self.c and self.a+self.c!=self.b and self.b+self.c!=self.a and self.c-self.b < self.a < self.c+self.b:
return True
else: return False
def get_angles(self):
"""Calculates the Angles based on the Cosine Rule."""
if self._istriangle():
self.A = degrees(acos((self.b**2+self.c**2-self.a**2)/float(2*self.b*self.c)))
self.B = degrees(acos((self.a**2+self.c**2-self.b**2)/float(2*self.a*self.c)))
self.C = 180 - (self.A+self.B)
print "It is a Triangle!"
print "Angles Are:\nA: %.2f\nB: %.2f\nC: %.2f" %(self.A,self.B,self.C)
else: print "Not-A-Triangle"
if __name__ == '__main__':
a,b,c = (float(x) for x in raw_input("Enter Sides(Separated by a Space): ").split())
t = Triangle(a,b,c)
t.get_angles()