d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Help With Python > Please :)
1235Next
Add Reply New Topic New Poll
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 9 2015 07:18pm
I have to write a code that will take 3 numbers as inputs and tell you whether or not it creates a right triangle.

I know i need to request the 3 inputs, and than write some kind of if else statement? thats about all i know.



ps: thanks
Member
Posts: 13,578
Joined: Jul 27 2010
Gold: 2,285.00
Feb 9 2015 07:31pm
You might want to try something like this:

https://docs.python.org/2/library/functions.html#raw_input

Code
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."


This post was edited by bentherdonethat on Feb 9 2015 07:42pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 04:18am
Quote (bentherdonethat @ Feb 9 2015 07:31pm)
You might want to try something like this:

https://docs.python.org/2/library/functions.html#raw_input

Code
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
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 09:21am
Thank to both of you for the response.

I tried running both and both are getting caught up and I can't seem to find the problems. I am using the python 3.4.2 ? the syntax you guys use it a little different than what I am used to

Here is what I came up with so far... I'm just missing the piece that will sort square and test them.

Code
#Request inputs
sideA = float(input("Length of side 1: "))
sideB = float(input("Length of side 2: "))
sideC = float(input("Length of side 3: "))

#Sort and square the values, then test for pythaogrean triple.


#Display results
if
print("The values do not create a right triangle.")
else
print("The values do create a right triangle.")


Thanks again
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 09:29am
Quote (known954 @ Feb 10 2015 09:21am)
Thank to both of you for the response.

I tried running both and both are getting caught up and I can't seem to find the problems. I am using the python 3.4.2 ? the syntax you guys use it a little different than what I am used to

Here is what I came up with so far... I'm just missing the piece that will sort square and test them.

Code
#Request inputs
sideA = float(input("Length of side 1: "))
sideB = float(input("Length of side 2: "))
sideC = float(input("Length of side 3: "))

#Sort and square the values, then test for pythaogrean triple.

#Display results
if
print("The values do not create a right triangle.")
else
print("The values do create a right triangle.")


Thanks again


Oops, I will rewrite for Python 3

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 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


Tested working in 3.4.1

This post was edited by j0ltk0la on Feb 10 2015 09:37am
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 09:36am
Quote (j0ltk0la @ Feb 10 2015 11:29am)
Oops, I will rewrite for Python 3


here is what i have now its running
Code
#Request inputs
sideA = float(input("Length of side 1: "))
sideB = float(input("Length of side 2: "))
sideC = float(input("Length of side 3: "))

#Sort and square the values, then test for pythaogrean triple.
listOfSides = [sideA, sideB, sideC]
listOfSides.sort()
for i in range (0,len(listOfSides)):
listOfSides[i] **= 2

#Display results
if (listOfSides[2] == listOfSides[0] + listOfSides[1]):
print("These three values can create a right triangle.")
else:
print("These three values cannot create a right triangle.")



what i need to add is is a piece to make it not break when i enter a input that isnt a number... also can you explain what exactly the sort piece is doing? because it works no matter what order I am entering the values, I assume the sort piece is responsible for that.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 09:43am
Quote (known954 @ Feb 10 2015 09:36am)
here is what i have now its running
Code
#Request inputs
sideA = float(input("Length of side 1: "))
sideB = float(input("Length of side 2: "))
sideC = float(input("Length of side 3: "))

#Sort and square the values, then test for pythaogrean triple.
listOfSides = [sideA, sideB, sideC]
listOfSides.sort()
for i in range (0,len(listOfSides)):
listOfSides[i] **= 2

#Display results
if (listOfSides[2] == listOfSides[0] + listOfSides[1]):
print("These three values can create a right triangle.")
else:
print("These three values cannot create a right triangle.")


what i need to add is is a piece to make it not break when i enter a input that isnt a number... also can you explain what exactly the sort piece is doing? because it works no matter what order I am entering the values, I assume the sort piece is responsible for that.


Lol, okay I'll try, heh.

The piece you need to skip incorrect input is a try statement, the way I wrote it works in a normal function.

Code
def ask():
try:
a = float(input('Input side 1: '))
b = float(input('Input side 2: '))
c = float(input('Input side 3: '))
d = [self.a, self.b, self.c]
#return list(map(lambda x:x**2, sorted(d))) # lol
except (ValueError, SyntaxError, NameError):
print('Notta Number')



When you call ask() it will create four variables in it's scope, three of them based on input a, b, c and d will contain them all in a list to make them easier to call sorted and sum. Also, something you might have overlooked in his original code, he forces the input to be floating point.

This post was edited by j0ltk0la on Feb 10 2015 09:44am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 09:51am
Now let me try to explain this piece

Code
list(map(lambda x:x**2, sorted(d)))


You already know that d = [a, b, c], what I am basically doing is called a function map which will apply a function to everything within the iterable I specify.

map -> squared function -> sequence

Code
def squared(x):
return x ** 2


is similar to this

Code
squared = lambda x:x**2
list(map(squared, sorted(d)))


Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 09:57am
hm......I think that makes sense? :wallbash:

Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 09:59am
Quote (known954 @ Feb 10 2015 09:57am)
hm......I think that makes sense? :wallbash:


Well, did it work?
Go Back To Programming & Development Topic List
1235Next
Add Reply New Topic New Poll