python问题无法加载函数

时间:2017-09-10 23:23:23

标签: python

所以我正在使用mit计算机科学讲座来学习python,但是我无法推进功能讲座/作业。基本上我写了一个函数,旨在找到x的平方根。 sqrt(x)是我用来在python 2.7 shell中调用它的东西。最初我自己复制了它,但我遇到了问题,所以我从讲义中复制了这个功能,但我仍然遇到同样的问题。基本上在讲座中我们创建了找到平方根的功能。当我在终端中运行该文件时,它不会返回任何代码。只是把我送到下一行,就像什么都没发生一样。当我在python shell中执行sqrt(16)时,它会给出一个错误'sqrt' is not defined'。 这是视频https://www.youtube.com/watch?v=SXR9CDof7qw&list=PL57FCE46F714A03BC&index=4我所指的部分从11:38开始。这是直接从讲义中复制的功能。它的缩写与视频中的相同,所以我认为这不是问题。

def sqrt(x):
  """Returns the square root of x, if x is a perfect square.
       Prints an error message and returns None otherwise"""
  ans = 0
  if x >= 0:
      while ans*ans < x: ans = ans + 1
      if ans*ans != x:
          print x, 'is not a perfect square'
          return None
      else: return ans
  else:
        print x, 'is a negative number'
        return None
def f(x): 
    x=x+1
    return x

编辑:将@JaredJenson的代码复制并粘贴到python shell中,并在运行时收到此错误消息https://imgur.com/4W5EUVG。我上面发布的视频显示教授在记事本中编写他的函数,然后通过键入sqrt(16)在python shell中运行它。当我这样做时,我收到一条错误消息

Traceback (most recent call last):

File "<pyshell#23>", line 1, in <module>

 sqrt(16)

NameError: name 'sqrt' is not defined

我不确定哪里出错了。应该输出4.

3 个答案:

答案 0 :(得分:0)

让我们一步一步地了解如何使用Python shell,看看我们是否能找出你出错的地方。

  1. 你打开shell,例如从命令行python,或在IDLE或其他IDE中使用shell窗口。

  2. 您会看到>>>提示,并开始输入该功能的代码。

  3. 每次在输入功能时按Enter键,都会在下一行的开头看到...提示。

  4. 你让shell知道你已经完成了写这个功能,输入一个空行(只需在空行上再次输入)。

  5. 提示返回>>>

  6. 您尝试调用该函数。

  7. 根据您的澄清评论进行编辑:看起来您尝试做的是将代码保存在文件中,然后调用此处定义的函数。您的屏幕截图显示您正在使用&#34; Python shell&#34; (它显示您使用命令行)。目前,您的程序文件britney.py定义了函数,但没有对它们执行任何操作。

    您可以通过以下几种方法测试这些功能:

    • 您可以让文件在定义之后包含对顶级函数的调用。

    • 您可以启动shell(来自源文件所在的文件夹 - 仅使用python而不使用文件名),然后import 模块由您的文件定义(在给定的示例中,您将import britney)并从中调用函数(britney.sqrt(64))。

    • 您可以启动shell,然后直接import函数(from britney import sqrt)并调用它(sqrt(64))。

    • 您可以使用-i命令行选项(python -i britney.py)运行该文件,这样您就可以检查&#34;&#34;运行后的程序状态 - 你得到一个Python shell,所以在那之后你可以自己调用sqrt

答案 1 :(得分:0)

听起来您正在将功能定义在与执行它的位置不同的位置。这里有三个选项。

  1. 将整个sqrt函数复制并粘贴到python shell中,然后运行sqrt(16)。
  2. 如果你这样做,你的shell应该是这样的:

    def sqrt(x):
      """Returns the square root of x, if x is a perfect square.
           Prints an error message and returns None otherwise"""
      ans = 0
      if x >= 0:
        while ans*ans < x: ans = ans + 1
        if ans*ans != x:
          print x, 'is not a perfect square'
          return None
        else: return ans
      else:
        print x, 'is a negative number'
        return None
    
    print sqrt(16)
    

    将整个内容复制并粘贴到python 2.7 shell后,它应该在最后打印出4

    1. 将整个内容保存在文件中,然后运行文件
    2. 您需要做的就是将上面的代码复制并粘贴到一个文件中(让我们称之为mysqrt.py),然后运行

      python mysqrt.py
      

      在命令行上

      1. 导入文件并从shell运行命令
      2. 除了从文件中删除最后一行(print sqrt(16))之外,你在这里做的与#2相同。

        然后,保存您的文件,并确保您已进入该文件所在的同一目录。

        启动python解释器

        python
        

        导入你的功能

        >>> from mysqrt import sqrt
        

        然后运行它

        >>> print(sqrt(16))
        

答案 2 :(得分:-1)

def sqrt(x):
    ans = 0
    if x >= 0:
        while ans*ans < x: ans = ans + 1
        if ans*ans != x:
             print(x, 'is not a perfect square')
             return None
        else:
             return ans
    else:
        print(x, 'is a negative number')

print(sqrt(4))      
print(sqrt(5))

>>>
2
5 is not a perfect square
None   

在Python3上看起来对我有用,请确保缩进