d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Help With Python > Please :)
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 05:46pm
Quote (bentherdonethat @ Feb 10 2015 07:24pm)
Now you're thinking like a software tester.


Thanks :)

but seriously lol what should i do about that?

Code
class RightTriangle(object):

def __init__(self):
self.a = 0
self.b = 0
self.c = 0
self.d = [self.a, self.b, self.c]

def ask(self):
try:
self.a = float(input('Length of side 1: '))
self.b = float(input('Length of side 2: '))
self.c = float(input('Length of side 3: '))
self.d = [self.a, self.b, self.c]
except (ValueError, SyntaxError, NameError):
print('Invalid input')


def square(self):
self.d = list(map(lambda x:x**2, sorted(self.d)))


def check(self):
self.answer = 'These three values can create a right triangle.' if self.d[-1] == sum([self.d[0], self.d[1]]) else 'These three values cannot create a right triangle.'
return self.answer


if __name__ == "__main__":
while 1:
try:
rt = RightTriangle()
rt.ask()
rt.square()
print(rt.d)
print(rt.check())
except KeyboardInterrupt:
pass
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 02:28am
'0.0' not in self.d

I believe, will do it
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 05:10am
Revision for 0.0

Code
class RightTriangle(object):

def __init__(self):
self.a = 0
self.b = 0
self.c = 0
self.d = [self.a, self.b, self.c]

def ask(self):
try:
self.a = float(input('Input side 1: '))
self.b = float(input('Input side 2: '))
self.c = float(input('Input side 3: '))
self.d = [self.a, self.b, self.c]
except (ValueError, SyntaxError, NameError):
print('Notta Number')


def square(self):
self.d = list(map(lambda x:x**2, sorted(self.d)))


def check(self):
self.answer = 'Right Triangle' if 0.0 not in self.d and self.d[-1] == sum([self.d[0], self.d[1]]) else 'Not a right Triangle'
return self.answer


if __name__ == "__main__":
while 1:
try:
rt = RightTriangle()
rt.ask()
print('Before:')
print(rt.d)
rt.square()
print('After:')
print(rt.d)
print(rt.check())
except KeyboardInterrupt:
pass



You will notice I changed check() and added0.0 not in self.d as a requirement for the first condition, anything else will evaluate to False.

Output:

Code
[j0ltk0la@heretic] $ python right_triangle.py
Input side 1: 3
Input side 2: 0
Input side 3: 3
Before:
[3.0, 0.0, 3.0]
After:
[0.0, 9.0, 9.0]
Not a right Triangle
Input side 1: 3
Input side 2: 3
Input side 3: 3
Before:
[3.0, 3.0, 3.0]
After:
[9.0, 9.0, 9.0]
Not a right Triangle
Input side 1: 3
Input side 2: 4
Input side 3: 5
Before:
[3.0, 4.0, 5.0]
After:
[9.0, 16.0, 25.0]
Right Triangle
Input side 1: 0
Input side 2: 0
Input side 3: 0
Before:
[0.0, 0.0, 0.0]
After:
[0.0, 0.0, 0.0]
Not a right Triangle


This post was edited by j0ltk0la on Feb 11 2015 05:36am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 06:50am
Another problem I noticed is it doesn't error when you enter in negative values, which shouldn't be possible on a Triangle, :rofl:

You could put in a condition to make sure inputs in self.d are greater than 0 or call the user an idiot and force them to be positive with abs.
Code

Input side 1: abs(-7)
Input side 2: -7
Input side 3: -7
Before:
[7.0, -7.0, -7.0]
After:
[49.0, 49.0, 49.0]
Not a right Triangle
Input side 1: -3
Input side 2: -4
Input side 3: -5
Before:
[-3.0, -4.0, -5.0]
After:
[25.0, 16.0, 9.0]
Not a right Triangle
Input side 1: 3
Input side 2: 4
Input side 3: 5
Before:
[3.0, 4.0, 5.0]
After:
[9.0, 16.0, 25.0]
Right Triangle
Input side 1: abs(-3)
Input side 2: abs(-4)
Input side 3: abs(-5)
Before:
[3.0, 4.0, 5.0]
After:
[9.0, 16.0, 25.0]
Right Triangle
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 11 2015 09:35am
Ok so i added

Code
if 0.0 not in self.d and


and it took care of the zero/invalid input issue. :thumbsup:

tried toying with adding
Code
if self.d>0 and


in the same line? or is the syntax wrong?

it keeps getting hung up when it has to spit out the answer, it accepts the 3 inputs.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 09:57am
Quote (known954 @ Feb 11 2015 09:35am)
Ok so i added

Code
if 0.0 not in self.d and


and it took care of the zero/invalid input issue. :thumbsup:

tried toying with adding
Code
if self.d>0 and


in the same line? or is the syntax wrong?

it keeps getting hung up when it has to spit out the answer, it accepts the 3 inputs.


Honestly, you might want to rewrite the syntax to be more traditional.

Code
def check(self):

if 0.0 not in self.d and self.d[-1] == sum([self.d[0], self.d[1]]):
self.answer = 'Right Triangle'
return self.answer

else:
self.answer = 'Not a right Triangle'
return self.answer


Keep in mind self.d is a list, it's len(self.d) will be equal to 3, the value of the list itself cannot be put against an integer value 0 because it isn't an integer, it is a list.

Code
>>>
>>> list_of_zeros = [0.0, 0.0, 0.0]
>>> bool(list_of_zeros)
True
>>> len(list_of_zeros)
3
>>> list_of_zeros > 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: list() > int()
>>>


self.a, self.b, andself.b will have to be greater than 0 when check() runs.

Haven't tested this code but it is along the same lines as what you would want, check if above 0, if not alert the user to enter valid sized triangle sizes, haha which would be a real number, and also have it continue to do the normal checks if above 0.

Code
for each_value in self.d:
if each_value > 0:
if self.d[-1] == sum([self.d[0], self.d[1]]):
self.answer = "Right Triangle"
return self.answer

else:
self.answer= "Not Right"
return self.answer
else:
return "Enter Valid Length Sides"


This post was edited by j0ltk0la on Feb 11 2015 10:21am
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 11 2015 11:21am
I get what you are saying about self.d being a list and you can't compare a list vs a integer.

However, all the defs ; checks; and indents have my mind fucked lol.

would you consider the original structure to be more traditional or more of a personal just-get-it-done style?

Either way i removed a few of the print lines and it seems to be good enough as is.

output:
Code
Length of side 1: 3
Length of side 2: 4
Length of side 3: 5
These three values can create a right triangle.
Length of side 1: 0
Length of side 2: 0
Length of side 3: 0
These three values cannot create a right triangle.
Length of side 1: 3
Length of side 2: /
Invalid input
These three values cannot create a right triangle.
Length of side 1: -3
Length of side 2: -4
Length of side 3: -5
These three values cannot create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: -5
These three values cannot create a right triangle.


This post was edited by known954 on Feb 11 2015 11:22am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 11:49am
Lol, probably, the style I wrote it in are what your programs will look like in a year.

Get used to the indents, 4-spaces each, don't use TAB.

You will come to appreciate using functions (e.g., def, lambda, etc.) to keep your code separated and clean.

Think of each section as a mini program in itself, each with it's own task and you put them together to get the complete result.
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 11 2015 12:15pm
You indent with 4 spaces? never tab? not even setting tab to 4 spaces? why?
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 11 2015 12:29pm
Quote (known954 @ Feb 11 2015 12:15pm)
You indent with 4 spaces? never tab? not even setting tab to 4 spaces? why?


That works too, as long as they're interpreted spaces, you can make your editor show them to be sure. Mixing tabs and spaces causes problems
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll