[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

Postby reggaelizard » Fri Jul 26, 2013 10:37 pm

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 testEqual
import math

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


Thanks in advance!
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
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Fri Jul 26, 2013 11:28 pm

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby micseydel » Fri Jul 26, 2013 11:33 pm

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.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Fri Jul 26, 2013 11:57 pm

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 testEqual

def 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 statement
File ".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.
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Sat Jul 27, 2013 12:21 am

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)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Sat Jul 27, 2013 12:40 am

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?
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby ochichinyezaboombwa » Sat Jul 27, 2013 12:45 am

As an alternative (and as a bonus learning material), write these two functions:
Code: Select all
def max_2(a,b):
    # your code here

def 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: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Sat Jul 27, 2013 12:57 am

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Sat Jul 27, 2013 1:04 am

Full code:
Code: Select all
from test import testEqual

def 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".
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Sat Jul 27, 2013 1:11 am

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 testEqual

def 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)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Sat Jul 27, 2013 1:14 am

Added the brackets, still getting the same message.
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Sat Jul 27, 2013 1:27 am

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
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Sat Jul 27, 2013 1:32 am

I get this error message:
Code: Select all
NameError: name '__builtins__' is not defined
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Sat Jul 27, 2013 1:44 am

Very strange i dont see how abs was working as that comes from the builtins and now its saying you have no builtins.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Help with right angled triangle function - Complete begi

Postby ochichinyezaboombwa » Mon Jul 29, 2013 6:05 am

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: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Help with right angled triangle function - Complete begi

Postby reggaelizard » Mon Jul 29, 2013 3:30 pm

I never had any problems with testing equalities, it's 'sorted' that isn't working.
User avatar
reggaelizard
 
Posts: 9
Joined: Fri Jul 26, 2013 3:11 am

Re: Help with right angled triangle function - Complete begi

Postby Yoriz » Mon Jul 29, 2013 5:40 pm

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 4 guests