## [ActiveCode] Help with right angled triangle function

This is the place for queries that don't fit in any of the other categories.

### [ActiveCode] Help with right angled triangle function

Hello! I'm pretty much new to computer science and I started learning Python 3 a few days ago with this guide. I've mostly gone through it rather smoothly, but now I have to write a function that takes the three sides of a triangle in any order and returns True if it's right angled and False if it isn't. The previous exercise was to write the same function but only for the sides given with the hypothenuse as the last one and I quickly sussed it out, but now I'm stuck on how to make it work with the sides given in any order; I know you can use max(args) to return the hypothenuse, though I don't know how to use it with the other two sides inside the function. I tried reading the documentation, but since I'm very inexperienced I was lost as to where to even look for the right module/function.

Here's my previous programme:
Code: Select all
`from test import testEqualimport mathdef is_rightangled(a,b,c):    sumSquaredSides = (a**2 + b**2)    squaredHypot = c**2    if abs(sumSquaredSides-squaredHypot) < 0.001:        return(True)    return(False)testEqual(is_rightangled(1.5,2.0,2.5), True)testEqual(is_rightangled(4.0,8.0,16.0), False)testEqual(is_rightangled(4.1,8.2,9.1678787077), True)testEqual(is_rightangled(4.1,8.2,9.16787), True)testEqual(is_rightangled(4.1,8.2,9.168), False)testEqual(is_rightangled(0.5,0.4,0.64031), True)`

Last edited by Yoriz on Mon Jul 29, 2013 6:44 pm, edited 1 time in total.
Reason: Added to title that this is programmed in ActiveCoode

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

To get your 3 values sorted so the largest is always in variable c you could use the following.
Code: Select all
`a, b, c = sorted((a, b, c))`

Then you can put this just inside the start of your function and it will work when the hypotenuse is passed in any position.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

Alternatively, using max, you could do
Code: Select all
`hypotenuse = max(a, b, c)`

and then have a bunch of if/else checking the hypotenuse against those values, and then reordering them depending on which is the hypotenuse. Yoriz' solution is definitely better than max though, and if you're not sure what it's doing, look up "argument unpacking".
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.

micseydel

Posts: 1930
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Help with right angled triangle function - Complete begi

Thanks to both of you! I followed Yoriz' solution and apparently there's no problem with it, since there are no errors found in the function itself. This is what it looks like right now:

Code: Select all
`from test import testEqualdef is_rightangled(a,b,c):    a, b, c = sorted(a,b,c)    sumSquaredSides = (a**2 + b**2)    squaredHypot = c**2    if abs(sumSquaredSides-squaredHypot) < 0.001:        return(True)    return(False)testEqual(is_rightangled(1.5,2.0,2.5), True)testEqual(is_rightangled(16.0,4.0,8.0, False)testEqual(is_rightangled(4.1,9.1678787077,8.2), True)testEqual(is_rightangled(9.16787,4.1,8.2), True)testEqual(is_rightangled(4.1,8.2,9.168), False)testEqual(is_rightangled(0.5,0.64031,0.4), True)`

However, it keeps telling me that there's a parse error due to bad input on line 13, which is one of the equalities to be tested. I figured it had to be because of the large number of decimal digits, so I deleted all but the first two equalities, and now I'm getting this:

Code: Select all
`TokenError: EOF in multi-line statementFile ".py", line 13^`

If I don't test any equalities, it runs with no compiling or runtime errors, though of course it doesn't print out anything.

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

You have a close bracket missing from this line
Code: Select all
`testEqual(is_rightangled(16.0,4.0,8.0, False)`
it should be
Code: Select all
`testEqual(is_rightangled(16.0,4.0,8.0), False)`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

Oh, thanks a lot! I can't believe I didn't notice it, I guess since the list already came with the tutorial I just assumed it didn't have any errors. I added the missing bracket and there weren't any more parse errors, but I got this message:

Code: Select all
`NameError: name 'sorted' is not defined`

According to the documentation, sorted() is a built-in function, so I reckon I shouldn't need to import any modules. Could it have something to do with the fact that I'm using activecode?

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

As an alternative (and as a bonus learning material), write these two functions:
Code: Select all
`def max_2(a,b):    # your code heredef max_3(a,b,c):    # your code here     # you should NOT use if;     # rather, use only your max_2(a,b) defined above.`

Code: Select all
`Test them like this:testEqual(max_3(1,2,3), 3)testEqual(max_3(2,3,1), 3)testEqual(max_3(3,1,2), 3)`
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: Help with right angled triangle function - Complete begi

The bracket was there in your first post, i think it must of fell off when you switched around the arguments order.
Yes sorted is a builtin so , i dont know why you would be having NameError's from it, maybe post the full code you have now so we can see if something else needs a little tweaking.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

Full code:
Code: Select all
`from test import testEqualdef is_rightangled(a,b,c):    a, b, c = sorted((a,b,c))    sumSquaredSides = (a**2 + b**2)    squaredHypot = c**2    if abs(sumSquaredSides-squaredHypot) < 0.001:        return(True)    return(False)testEqual(is_rightangled(1.5,2.0,2.5), True)testEqual(is_rightangled(16.0,4.0,8.0), False)testEqual(is_rightangled(4.1,9.1678787077,8.2), True)testEqual(is_rightangled(9.16787,4.1,8.2), True)testEqual(is_rightangled(4.1,8.2,9.168), False)testEqual(is_rightangled(0.5,0.64031,0.4), True)`

This gives me the message "name 'sorted' is not defined".

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

I dont have your testEqual module so i commented that and the tests out.
It gives me a different error to yours
Code: Select all
`a, b, c = sorted(a,b,c)TypeError: 'float' object is not iterable`
That is because a, b, c should be surounded by brackets like in my earlier post
You code should look like this
Code: Select all
`from test import testEqualdef is_rightangled(a,b,c):    a, b, c = sorted((a,b,c))    sumSquaredSides = (a**2 + b**2)    squaredHypot = c**2    if abs(sumSquaredSides-squaredHypot) < 0.001:        return(True)    return(False)testEqual(is_rightangled(1.5,2.0,2.5), True)testEqual(is_rightangled(16.0,4.0,8.0), False)testEqual(is_rightangled(4.1,9.1678787077,8.2), True)testEqual(is_rightangled(9.16787,4.1,8.2), True)testEqual(is_rightangled(4.1,8.2,9.168), False)testEqual(is_rightangled(0.5,0.64031,0.4), True)`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

Added the brackets, still getting the same message.

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

bizzare
try puting this line
Code: Select all
`print 'sorted' in dir(__builtins__)`

before and after your import line
Code: Select all
`from test import testEqual`

does it give true both times
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

I get this error message:
Code: Select all
`NameError: name '__builtins__' is not defined`

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

Very strange i dont see how abs was working as that comes from the builtins and now its saying you have no builtins.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Help with right angled triangle function - Complete begi

Change
Code: Select all
`from test import testEqual                                                                                                                                                         `

to
Code: Select all
`# from test import testEqual                                                                                                                                                         def testEqual(a,b):    print "TEST result is:", a == b`
, then run it again and let us know what happens.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: Help with right angled triangle function - Complete begi

I never had any problems with testing equalities, it's 'sorted' that isn't working.

reggaelizard

Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

### Re: Help with right angled triangle function - Complete begi

It looks like your doing a course that you run python code in a browser and it doesnt support everything real python does.

Edit found this:

Note: Python modules and limitations with activecode

Throughout the chapters of this book, activecode windows allow you to practice the Python that you are learning. We mentioned in the first chapter that programming is normally done using some type of development environment and that the activecode used here was strictly to help us learn. It is not the way we write production programs.

To that end, it is necessary to mention that many of the modules available in standard Python will not work in the activecode environment. In fact, only turtle, math, and random have been ported at this point. If you wish to explore any additional modules, you will need to also explore using a more robust development environment.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1376
Joined: Fri Feb 08, 2013 1:35 am
Location: UK