[Tutor] cs student needs help import math

Chris Down chris at chrisdown.name
Sun Sep 8 02:16:15 CEST 2013


On 2013-09-07 17:02, Byron Ruffin wrote:
> I am writing a simple program based off an ipo chart that I did correctly.
> I need to use ceil but I keep getting an error saying ceil is not defined.
> I did import math, I think.  I am using 3.2.3 and I imported this way...
>
> >>> import math
> >>> math.pi
> 3.141592653589793
> >>> math.ceil(math.pi)
> 4
> >>> math.floor(math.pi)
> 3
>
> ... but I get the error when using ceil...
>
> pepsticks = ceil(peplength / StickLength)
> Traceback (most recent call last):
>   File "<pyshell#19>", line 1, in <module>
>     pepsticks = ceil(peplength / StickLength)
> NameError: name 'ceil' is not defined

The error message is pretty clear, "ceil" is not defined. If you started by
using "import math", this is expected, because you need to explicitly call
"math.ceil", not just "ceil". If you want to import ceil into your current
namespace, you need to use "from":

    >>> from math import ceil
    >>> ceil(3.14)
    4
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20130908/f96ca178/attachment.sig>


More information about the Tutor mailing list