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: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 10:12am
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.")


Your code, I rewrote it

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]

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

#Display results
yes = "Yes, these three values %.2f, %.2f, %.2f can create a right triangle" % (squared[0], squared[1], squared[-1])
no = "No, these three values %.2f, %.2f, %.2f cannot create a right triangle" % (squared[0], squared[1], squared[-1])
print(yes) if squared[-1] == sum([squared[0], squared[1]]) else no


This post was edited by j0ltk0la on Feb 10 2015 10:22am
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 10:12am
edit: nvm

This post was edited by known954 on Feb 10 2015 10:18am
Member
Posts: 13,578
Joined: Jul 27 2010
Gold: 2,285.00
Feb 10 2015 10:13am
Quote (j0ltk0la @ Feb 10 2015 06:18am)
Why not something straight forward and functional?


Because someone who has trouble with this problem probably isn't ready to dive into a more complex OO approach. Gotta learn to walk before you can run, eh?
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 10:14am
Quote (bentherdonethat @ Feb 10 2015 10:13am)
Because someone who has trouble with this problem probably isn't ready to dive into a more complex OO approach. Gotta learn to walk before you can run, eh?


Gotta learn to skin your knees real fooken bad and keep running while bleeding everywhere.
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 10:17am
a understanding of the basics is important, but i would like to understand the more advance techniques as well... gota learn them eventually.


and I ran what you rewrote it works.

the squared = list part, it seems to me the list goes from -1 to 1? where as the sort created something from 0 to 2? this confuses me
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 10:23am
Quote (known954 @ Feb 10 2015 10:17am)
a understanding of the basics is important, but i would like to understand the more advance techniques as well... gota learn them eventually.


and I ran what you rewrote it works.

the squared = list part, it seems to me the list goes from -1 to 1? where as the sort created something from 0 to 2? this confuses me


-1 is the last index of a list, tuple, or anything. Since you know the length of your list will be list(range(0, 3)) -> [0, 1, 2] there isn't a need to worry about using -1 or 2, they're both the same value. The reason I threw it in there is so you can learn about counting indexes backwards using -1, -2, -3, etc.
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 10:28am
Ok... so if the list ranges from [0,3] than python will recognize [0,1,2] and [-1,0,-1] as the same thing?



also... the code you wrote doesnt work if you enter an invalid character
or if the else statement is needed? it won't output anything if you enter a non-triple like 3, 4, 6
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 10:30am
Quote (known954 @ Feb 10 2015 10:28am)
Ok... so if the list ranges from [0,3] than python will recognize [0,1,2] and [-1,0,-1] as the same thing?



also... the code you wrote doesnt work if you enter an invalid character
or if the else statement is needed? it won't output anything if you enter a non-triple like 3, 4, 6


It will recognize list[0], list[1], list[2] the same as it recognizes list[-3], list[-2], list[-1] and other combinations you can think of.

Seems fine for me, the problem you're having is the try statement isn't available in this short version I posted, if you want that you will have to implement it like done on the first page.
:(

Code
[j0ltk0la@heretic] $ python -i test.py
Length of side 1: 3
Length of side 2: 4
Length of side 3: 5
Yes, these three values 9.00, 16.00, 25.00 can create a right triangle
>>> exit()
[j0ltk0la@heretic] $ python -i test.py
Length of side 1: 4
Length of side 2: 5
Length of side 3: 6
No, these three values 16.00, 25.00, 36.00 cannot create a right triangle
>>>


This post was edited by j0ltk0la on Feb 10 2015 10:32am
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 10:50am
tried applying the try statement I can't seem to get the syntax right, something is always not defined or there is an invalid indent.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 10:55am
Quote (known954 @ Feb 10 2015 10:50am)
tried applying the try statement I can't seem to get the syntax right, something is always not defined or there is an invalid indent.


Code
try:
[code]
[code]
[code]
except ErrorName:
action


try and except are on the same block, the code and action for whatever the error is called, which you will have to make sure you're using the right one, unless you make your own, which is another topic in itself.

https://docs.python.org/3/tutorial/errors.html

This post was edited by j0ltk0la on Feb 10 2015 10:56am
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll