[Tutor] hypotenuse
Robert Childers
rbtchilders at gmail.com
Fri Mar 14 17:52:26 CET 2008
I have rewritten my "hypotenuse" program as follows:>>> #This program
calculates the width and diagonal of a golden rectangle
>>> print "Calculate the width and diagonal of a golden rectangle."
Calculate the width and diagonal of a golden rectangle.
>>> height = input ("Input height:")
Input height:1
>>> width = height*1.618
>>> print "Width:", width
Width: 1.618
>>> import math
>>> hyp_squared = height**2 + width**2
>>> hypotenuse = math.sqrt(hyp_squared)
>>> print "Diagonal:", hypotenuse
Diagonal: 1.90208412012
When I save the program then try to run the module I get an error message
that it is invalid.
I have been assuming that if no "invalid syntax" error occurs in the shell
the program should run. Could this problem be associated with the import
math instruction?
On Fri, Mar 14, 2008 at 6:03 AM, Alan Gauld <alan.gauld at btinternet.com>
wrote:
> "Wolfram Kraus" <wolfram.kraus at fen-net.de> wrote in message
> news:frdiev$m4u$1 at ger.gmane.org...
> > Am 14.03.2008 09:40, Alan Gauld schrieb:
> >>>> Why can you not use something like:
> >>>>
> >>>>>>> hypotenuse = hyp_squared**1/2
> >>
> >> And for completeness that could also be written:
> >>
> >> hypotenuse = pow(hyp_squared,1/2)
> >>
> >> Again, without the need to import math.
> >>
> > But beware of the integer divison in Python:
> >
> > >>> pow(2,1/2)
> > 1
>
> Good catch. Applies to the ** style too.
>
> Alan G
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080314/27d6f841/attachment.htm
More information about the Tutor
mailing list