【问题标题】:What does the error "Name Error: name is not defined" mean in this code [duplicate]此代码中的错误“名称错误:名称未定义”是什么意思[重复]
【发布时间】:2019-09-10 14:45:10
【问题描述】:

考虑以下代码:

def distancias(altitudes,lado,p1,p2):
    p1=(r1,c1)
    p2=(r2,c2)
    a1=altitudes[p1]
    a2=altitudes[p2]
    d=math.sqrt((lado(r1-r2)**2)+(lado(c1-c2)**2)+(a1-a2)**2)
    return d

Altitudes 是一个矩阵,p1 和 p2 是矩阵的元素。

当我调用函数distancias(teste, 20, (2, 0), (3, 1)) 时,它给了我这个错误:

Traceback (most recent call last):

  File "<ipython-input-11-6c6a4bb1ff77>", line 1, in <module>
    distancias(teste, 20, (2, 0), (3, 1))

  File "C...", line 5, in distancias
    p1=(r1,c1)

NameError: name 'r1' is not defined

    "Name Error: name is not defined".

(我想更好地理解它,因为我是 Python 新手)

【问题讨论】:

  • 你还没有发布所有代码(这个错误不可能来自这个sn-p!)
  • 除非“名称”实际上是 r1 并且您在解释。发布实际错误。
  • 欢迎来到 StackOverflow。为了帮助您解决错误,您需要向我们展示一个完整的代码 sn-p,它会在我们运行该代码时显示该错误。请阅读并关注How to create a Minimal, Complete, and Verifiable example。
  • 回溯是什么?
  • 总是将完整的错误消息(Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。

标签: python python-3.x


【解决方案1】:

你好,这是因为 "r1,c1,r2,c2" 不是函数 distancias 的定义。 所以使用应该在使用它们之前定义这些变量。实际上,您应该发布完整的代码以更好地了解其他人。但是如果您可以使用此部分。您应该更改类似的内容。

     `def distancias(altitudes,lado,r1,r2,c1,c2):
      p1=(r1,c1)
      p2=(r2,c2)
      a1=altitudes[p1]
      a2=altitudes[p2]
      d=math.sqrt((lado(r1-r2)**2)+(lado(c1-c2)**2)+(a1-a2)**2)
      return d`

【讨论】:

  • 嗯,没有。函数可以访问不作为参数提供给它的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多