Quote (bentherdonethat @ Feb 9 2015 07:31pm)
You might want to try something like this:
https://docs.python.org/2/library/functions.html#raw_inputCode
try:
a = float(raw_input('Input side 1: '))
b = float(raw_input('Input side 2: '))
c = float(raw_input('Input side 3: '))
except ValueError:
print "You entered something that can't be interpreted as a number!"
Keep in mind that the user doesn't need to type in a, b, and c in any particular order. You'll need to determine which is the biggest number, and plug that into the pythagorean theorem. So in this case, a, b, and c are probably not the best choices for variable names, even though that's what's used in the formula.
-----
Edit: Kept on going, because why not. Put the three variables into a list, then sort it. Square each of the elements in the list. Then if the biggest one (now the last element of the list) is equal to the sum of the 0th and 1st element in the list, then you have a pythagorean triple.
Code
list_of_side_lengths = [a, b, c]
list_of_side_lengths.sort()
for i in range(0,len(list_of_side_lengths)):
list_of_side_lengths[i] **= 2
if (list_of_side_lengths[2] == list_of_side_lengths[0] + list_of_side_lengths[1]):
print "This is a right triangle."
else:
print "This is not a right triangle."
Why not something straight forward and functional?
Code
squared = map(lambda x:x**2, sorted(list_of_sides))
print('Right Triangle') if squared[-1] == sum([squared[0], squared[1]]) else 'Not Right'
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 = map(lambda x:x**2, sorted(self.d))
#return self.d
def check(self):
self.answer = 'Right Triangle' if 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
Output:
Code
[j0ltk0la@heretic] $ python -i right_triangle.py
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: 4
Input side 2: 3
Input side 3: 5
Before:
[4.0, 3.0, 5.0]
After:
[9.0, 16.0, 25.0]
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: 6
Input side 2: 6
Input side 3: 6
Before:
[6.0, 6.0, 6.0]
After:
[36.0, 36.0, 36.0]
Not a right Triangle
This post was edited by j0ltk0la on Feb 10 2015 04:43am