[Tutor] hypotenuse

John Fouhy john at fouhy.net
Fri Mar 14 02:13:32 CET 2008

On 14/03/2008, Robert Childers <rbtchilders at gmail.com> wrote:
> I am in an early lesson in "A Byte of Python."  Instead of writing a program
> to find the area of a rectangle I thought it would be useful to write a
> program to determine the length of the diagonal of a "golden rectangle",
> which would of course equal the sq root of the sum of the squares of the
> width and height.  Here is my program:
> >>> height = input ("Height:")
> Height:1
> >>> width = input ("Width:")
> Width:1.618
> >>> int = ((height**2) + (width**2))
> >>> print int
> 3.617924
>  >>> hypotenuse * hypotenuse = int
> SyntaxError: can't assign to operator
> I looked ahead in the lesson and could find no mention of square roots.  How
> do I find the square root of an integer?

Hi Robert,

This kind of thing:

 >>> hypotenuse * hypotenuse = int

will never work.  The thing on the left side of an equals sign must
always be a single name.
(there is an exception to this -- "unpacking" -- but I won't explain
it now.  You should come to it in time)

Python provides a square root function, but it's not available by
default.  You need to import the math module first -- your tutorial
should cover importing.  Basically, the code will look something like

>>> import math
>>> hyp_squared = height**2 + width**2
>>> hypotenuse = math.sqrt(hyp_squared)

Finally, "int" is a built-in type, so it's bad programming style to
use it as a variable name. (the same goes for "list", "str", and a few
others)  That's why, in my example, I used "hyp_squared" as the name
for the hypotenuse squared.


More information about the Tutor mailing list