David Lemper wrote:
I'm a newbee trying 3.0 Please help with math.sqrt() At the command line this function works correctly >>> import math
              n = input("enter a number > ")
              s = math.sqrt(n)
     An entry of 9 or 9.0  will yield 3.0

Yet the same code in a script gives an error message
     Script1
                   import math
                   n = input("enter a number > ")
                   s = math.sqrt(n)
 ... TypeError : a float is required
>
Strangely the above code runs fine in version 2.5  ( ? ) ...

OK, here's what's going on:
"at the command line" in 2.X, the builtin function input reads a string
and returns the eval of that string.  This is a bit of a safety issue.
I suspect when "it worked from the command line," you were using a 2.X
command line inadvertently.  in 2.X, you'll get similar errors if you
use "raw_input" instead of "input".

The "input" function in 3.0 is the same as the "raw_input" function
in 2.X.  I would suggest using:
    import math
    value = float(input("enter a number > "))
    root = math.sqrt(value)
    print('root(%s) == %s' % (value, root))

I avoid using single-letter variables except where I know the types
from the name (so I use i, j, k, l, m, n as integers, s as string,
and w, x, y, and z I am a little looser with (but usually float or
complex).

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to