math.sqrt() in new 3.0 version : solution in input()

David David
Sat Dec 27 03:34:45 CET 2008


On Fri, 26 Dec 2008 15:52:24 -0600, David Lemper wrote:

>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)
>              
>               Traceback (most recent call last) :
>                  File "<stdin>", line 1, in <module>
>                  File "script1.py" line 3 in <module>
>                     s = math.sqrt(n)
>               TypeError : a float is required

Problem is the new input() function. Yields a string.

Thanks to Scott, Chris, Gabriel & John.

Some thought I was not using the Python 3 command line.
I was :  Python 3.0 (r30:67507...
Erratic behavior was that I was sometimes using n = input()
and sometimes entering the integer directly into the
math.sqrt() function, eg s = math.sqrt(4194304)

I cannot find a mention of this in "The Python Tutorial 
release 3.1"   The I&O section discusses output formatting
and reading & writing text files.
John pointed out its in Guido's "What's New in Python3.0"
Indeed its mentioned in PEP 3111, near end of What's New 
and somewhat obscurely. " raw_input() was renamed to input().
That is the new input() function function reads a line from
sys.stdin . . ."                          Dave Lemper, Texas




More information about the Python-list mailing list