我本周刚开始学习Python。 (我将在一个月内成为一名计算机科学新人!)
这是我编写的用于计算x的平方根的函数。
#square root function
def sqrt(x):
"""Returns the square root of x if x is a perfect square.
Prints an error message and returns none if otherwise."""
ans = 0
if x>=0:
while ans*ans <x:
ans = ans + 1
if ans*ans == x:
print(x, 'is a perfect square.')
return ans
else:
print(x, 'is not a perfect square.')
return None
else: print(x, 'is a negative number.')
但是当我保存它并在Python shell中键入sqrt(16)时,我收到一条错误消息。
NameError: name 'sqrt' is not defined
我正在使用Python 3.1.1。 我的代码有问题吗? 任何帮助,将不胜感激。 感谢
的更新 的 好的,多亏你们,我们才意识到我没有导入这个功能。 当我尝试导入它时,我收到一个错误,因为我将它保存在通用的My Documents文件而不是C:\ Python31中。所以在将脚本保存为C:\ Python31 \ squareroot.py之后,我输入了shell(重新启动它):
导入squareroot
又出现了一个新错误!
>>> import squareroot
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import squareroot
File "C:\Python31\squareroot.py", line 13
return ans
SyntaxError: 'return' outside function
意思是我的原始代码中存在错误!我现在要看下面的一些建议更正。如果您发现了其他任何事情,请说。谢谢:))
更新2 - 工作!!!!!!!!!!
这就是我的所作所为。 首先,我使用了IamChuckB发布的清理版代码。我在其中创建了一个新脚本(将函数名称从sqrt更改为sqrta以区分):
def sqrta(x):
"""Returns the square root of x if x is a perfect square.
Prints an error message and returns none if otherwise."""
ans = 0
if x>=0:
while ans*ans <x:
ans = ans + 1
if ans*ans == x:
print(x, 'is a perfect square.')
return ans
else:
print(x, 'is not a perfect square.')
return None
else:
print(x, 'is a negative number.')
而且,重要的是,将其保存为C:\ Python31 \ squareroota.py(再次,在末尾添加“a”以区分其他失败的文件。)
然后我重新打开了Python shell并做了这个:
>>> import squareroota
什么都没发生,没有错误,太棒了!然后我这样做了:
>>> squareroota.sqrta(16)
得到了这个!
16 is a perfect square.
4
哇。我知道这可能看起来像是在学校玩ABC积木,但它真的让我大吃一惊。非常感谢大家!
答案 0 :(得分:4)
是的我相信你必须将你的功能实际导入shell。
from yourfile import sqrt
小心点。我想如果你在shell中进行更改,则必须重新导入函数才能显示这些更改。正如delnan在下面提到的,你可以reload
更改后的文件..
答案 1 :(得分:1)
首先,你的循环总是在第一次迭代时结束,因为你基本上有if (...) return else return
。试试这个:
def sqrt(x):
"""Returns the square root of x if x is a perfect square.
Prints an error message and returns none if otherwise."""
ans = 0
if x >= 0:
while ans * ans <= x:
if ans * ans == x:
print(x, 'is a perfect square.')
return ans
ans = ans + 1
print(x, 'is not a perfect square.')
return None
else: print(x, 'is a negative number.')
但请注意,Python提供了内置的幂运算符:
def sqrt(x):
return x ** 0.5
要具体回答您的问题,您必须导入您的功能。如果编写该文件的文件是 sqrt.py ,要在另一个文件中使用此函数,则需要from sqrt import sqrt
。
答案 2 :(得分:0)
NameError
表示你的python shell无法识别该函数。您可能忘记导入脚本了。
假设您将文件保存为myscript.py
(与启动python shell的目录相同的目录),您必须使用:
import myscript
内部定义的函数可用。请注意,您必须调用myscript.sqrt
才能运行您的函数sqrt
:它仅在myscript
命名空间中可用。
另一种方法是键入from myscript import sqrt
:在这种情况下,您可以将myscript.sqrt
命名空间中的sqrt
用作from ... import ...
。请注意,不要使用此{{1}} ...
答案 3 :(得分:0)
这是您的原始代码,只是清理干净以便运行。最初格式化的问题是缩进。
while块应该缩进一级(4个空格),以表示它位于函数sqrt的def块中。
在while语句中包含if / else块意味着每次通过循环都会进行检查;因此,第一次当ans只等于1时,将完成测试,打印输出并返回一个值。我们需要改变这一点。其他几个答案提供了更直接的方法来在python中对此进行短语,但是,尽可能接近您编写的代码,您实际需要做的就是将if块和else块移出阻止。代码如下所示:
def sqrt(x):
"""Returns the square root of x if x is a perfect square.
Prints an error message and returns none if otherwise."""
ans = 0
if x>=0:
while ans*ans <x:
ans = ans + 1
if ans*ans == x:
print(x, 'is a perfect square.')
return ans
else:
print(x, 'is not a perfect square.')
return None
else:
print(x, 'is a negative number.')
显示示例输入和输出: 在:
sqrt(9)
输出:
9 is a perfect square.
在:
sqrt(8)
输出:
8 is not a perfect square.
编辑:在我看来,Python是一门优秀的第一语言。当我第一次开始使用它时,我发现MIT OpenCourseWare类非常有用。 一个非常重要的注意事项:使用Python 2.x而不是3.x教授课程,因此某些给定的代码无法正常使用。即使您没有观看所有视频讲座,assignments they give是一个合理的难度,reading assignments引用了一些优秀的蟒蛇学习材料。
Udacity CS101类还提供了一个很好的,直接的Python编程入门介绍(并使用Python 2.x ),但我只完成了大约一半的任务。不过,我还是建议看看它。