pow()有效,但sqrt()没有!? [英] pow() works but sqrt() not!?

查看:85
本文介绍了pow()有效,但sqrt()没有!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是一个新手问题:

Python 2.5(r25:51908,2006年9月19日,09:52:17) [MSC v.1310 32位(英特尔)]

win32

带WinXP的电脑



< a rel =nofollowhref =http://www.python.org/doc/2.3.5/lib/module-math.html\"target =_ blank> http://www.python.org/doc /2.3.5/lib/module-math.html

我读过:


" sqrt(x)返回平方根x。


现在用函数pow()测试模块数学:

-------------- -------------------------------------

< blockquote class =post_quotes>


>> pow(9,9)



387420489


很好,但是sqrt()失败了:

------------------ -


>> sqrt(9)



我收到此错误消息


''Traceback(最近一次调用最后一次):

文件"< pyshell#3>",第1行,在< module>

sqrt(9)

NameError:name''sqrt' '未定义''


sin()和cos()相同。 ">>导入数学"没有帮助。我是否必须首先定义sqrt()函数
?如果是这样,怎么样?


请帮忙!


谢谢,

Siggi



解决方案

你忘了导入数学模块


>> import math
math.sqrt(9)






如果你想将数学函数用于当前命名空间:

< blockquote class =post_quotes>


>>来自math import *



-

T?nis


1月4日上午10点13分,siggi < smusnmrNOS ... @ yahoo.comwrote:


大家好,


这是一个新手问题:

Python 2.5(r25:51908,2006年9月19日,09:52:17)[MSC v.1310 32位(英特尔)]

win32

带WinXP的电脑

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html

I阅读:


" sqrt(x)返回x的平方根。


现在用函数pow测试模块数学():

--------------------------------------- ------------>> pow(9,9)387420489


罚款,但sqrt()失败:

------------------->> sqrt(9)我收到此错误消息


''Traceback(大多数)最近的呼叫最后一次):

文件"< pyshell#3>",第1行,在< module>

sqrt(9)

NameError:name''sqrt''未定义''


sin()和cos()相同。 ">>导入数学"没有帮助。我是否必须首先定义sqrt()函数
?如果是这样,怎么样?


请帮帮忙!


谢谢,


Siggi


非常感谢,T?nis!:


*** 1 ***


>您忘记导入数学模块


>> import math



不,我没有!但我使用的是sqrt(9),而不是math.sqrt(9)。后者的作品

非常好,谢谢!从现在开始,我将使用导入数学。和

" math.fuction()"对于每个数学函数,甚至对于pow()等只是

为安全起见!


*** 2 ***


>如果您希望数学函数使用当前命名空间:


>>来自math import *



什么是命名空间? &data>>>导入数学>

和">>>来自math import *" ?


Siggi

" tonisk" < me ******* @ gmail.comschrieb im Newsbeitrag

news:11 ********************* @ 31g2000cwt .googlegrou ps.com ...

你忘了导入数学模块


>>导入数学
math.sqrt(9)



3.0


如果你想要数学函数到你当前的命名空间使用:


>>来自math import *



-

T?nis


1月4日上午10点13分,siggi < smusnmrNOS ... @ yahoo.comwrote:


大家好,


这是一个新手问题:

Python 2.5(r25:51908,2006年9月19日,09:52:17)[MSC v.1310 32 bit(Intel)]

on

win32

带WinXP的电脑

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html

我读过:


" sqrt(x)返回x的平方根。


现在用函数pow()测试模块数学:

------------------------------- -------------------->> pow(9,9)387420489


很好,但是sqrt()失败了:

------------------->> sqrt(9)我收到此错误消息


''Traceback(最近一次调用最后一次):

文件"< pyshell#3>",第1行,< module>

sqrt (9)

NameError:名称''sqrt''未定义''


sin()和cos()相同。 ">>导入数学"没有帮助。我是否必须首先定义sqrt()函数
?如果是这样,怎么样?


请帮帮忙!


谢谢,


Siggi



siggi写道:


什么是命名空间? &data>>>导入数学>

和">>>来自math import *" ?

http://preview.tinyurl.com/t4pxq


了解更多信息,*请*阅读教程中的相关部分。

Python'关于命名空间的全部内容,试验和错误是不太好用

的方式来计算它们是如何工作的。


< / F>


Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on
win32
PC with WinXP

In
http://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
---------------------------------------------------

>>pow(9,9)

387420489

Fine, but sqrt() fails:
-------------------

>>sqrt(9)

I get this error message

''Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name ''sqrt'' is not defined''

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi




解决方案

you forgot to import math module

>>import math
math.sqrt(9)

3.0

if you want math functions to your current namespace use:

>>from math import *

--
T?nis

On Jan 4, 10:13 am, "siggi" <smusnmrNOS...@yahoo.comwrote:

Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]on
win32
PC with WinXP

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
--------------------------------------------------->>pow(9,9)387420489

Fine, but sqrt() fails:
------------------->>sqrt(9)I get this error message

''Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name ''sqrt'' is not defined''

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi


Thank you very much, T?nis!:

*** 1 ***

>you forgot to import math module

>>import math

Nope, I did not! But I used sqrt(9), and not math.sqrt(9). The latter works
excellent, thank you! From now on, I will use "import math" and
"math.fuction()" for EVERY mathematical function, even for pow() etc. just
to be on the safe side!

*** 2 ***

>if you want math functions to your current namespace use:

>>from math import *

What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?

Siggi
"tonisk" <me*******@gmail.comschrieb im Newsbeitrag
news:11*********************@31g2000cwt.googlegrou ps.com...
you forgot to import math module

>>import math
math.sqrt(9)

3.0

if you want math functions to your current namespace use:

>>from math import *

--
T?nis

On Jan 4, 10:13 am, "siggi" <smusnmrNOS...@yahoo.comwrote:

Hi all,

this is a newbie question on :
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on
win32
PC with WinXP

Inhttp://www.python.org/doc/2.3.5/lib/module-math.html
I read:

"sqrt( x) Return the square root of x."

Now the test for module math with function pow():
--------------------------------------------------->>pow(9,9)387420489

Fine, but sqrt() fails:
------------------->>sqrt(9)I get this error message

''Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
sqrt(9)
NameError: name ''sqrt'' is not defined''

Same for sin() and cos(). ">>Import math" does not help. Will I have to
define the sqrt() function first? If so, how?

Please help!

Thank you,

Siggi



siggi wrote:

What is a "namespace" and what is the difference between ">>>import math"
and ">>>from math import *" ?

http://preview.tinyurl.com/t4pxq

for more on this, *please* read the relevant sections in the tutorial.
Python''s all about namespaces, and trial and error is not a very good
way to figure how they work.

</F>


这篇关于pow()有效,但sqrt()没有!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆