- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这里是 Python 新手。我正在编写一个类,该类具有计算两组坐标之间距离的方法。此方法采用 2 个参数:
这是我的代码:
import pymysql.cursors
class Test(object):
def __init__(self):
self.create_connection()
def create_connection(self):
self.conn = pymysql.connect(host='localhost',
user='root',
password='',
db='testDB',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()
def __del__(self):
self.c_coord()
self.q_coord()
self.calc_dist(cCoord, qCoord)
self.closeDB
def c_coord(self):
sql = "SELECT ID, coordinates from target where coordinates != 'NA'"
self.cursor.execute(sql)
# a dictionary cursor returns every row in the sql statement as a dictionary
cCoord = self.cursor.fetchall()
return cCoord
def q_coord(self):
sql = "SELECT station, coordinates from qSubway"
self.cursor.execute(sql)
qCoord = self.cursor.fetchall()
return qCoord
# return min subway and distance
def calc_dist(self, cCoord, qCoord):
print(cCoord)
print(qCoord)
def closeDB(self):
self.conn.close()
当我在 python 控制台中运行它时,这就是我得到的:
slsu = Test()
slsu.c_coord() [{'ID': 6221530552, 'coordinates': '40.745300,-73.861100'}, ...
slsu.q_coord() [{'station': '21st Street (IND Crosstown Line)', 'coordinates': '40.744591, -73.948674'}, ...
slsu.calc_dist(cCoord, qCoord) Traceback (most recent call last): File "", line 1, in NameError: name 'cCoord' is not defined
我需要一些帮助来理解这个错误以及如何修复它?我想如果你给函数传递一个参数,它应该会自动识别它?
最佳答案
您必须声明变量 cCoord 和 qCoord。函数不返回您可以使用的变量。将函数想象成一个黑盒子。它可以使用您给它的变量,但它所做的任何更改都不会影响该函数之外的任何内容。 return 命令只是意味着,如果您将变量设置为等于 c_Coord(),则该变量将具有函数返回的值。要解决此问题,请为您的两个 Coord 函数设置变量。
cCoord = c_Coord()
qCoord = q_Coord()
这两个函数都已运行,现在您可以在这些函数之外使用它们返回的内容。
关于Python:尝试将函数参数传递给类中的另一个函数,得到 NameError:未定义名称 ' ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179063/
虽然我试图完全理解 NoMethodError 和 NameError 之间的区别,但我发现 NameError 在祖先中不在 0 位置。是 DidYouMean 的魔法吗? [1] pry(main
请谁来帮我解决这个问题。**当在服务器上运行时,特定的结果会出现,但服务器运行得很好。当我运行代码时,它发送这个错误
下面的 Python 由于某种原因失败了。 class NetVend: def blankCallback(data): pass def sendCommand(c
作为this great answer建议,从 Python 3.7 开始,如果 from __future__ import annotations 使用指令。 但是,如果我想为注释类型创建别名,这
我在导入自制模块时遇到了一些麻烦,我只是看不出我做错了什么。 我有一个名为 basics 的包,其中包含我所有的基类 我有一个名为 components 的第二个包,components 中的每个模块
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
所以我是第一年,有一点编码经验,但不多,我被赋予了制作基于文本的冒险游戏的任务。我的讲师给了我们一个模板并帮助我们开始,但我很困惑为什么这段代码会导致 shell 显示 NameError: name
我有一些编程经验,但我对 python 很陌生,我正在尝试弄清楚如何使用和导入 .py 文件中的类而不是 main 。我目前正在使用 netbeans,运行 CPython 3.2.1。 根据我现在的
代码非常简单,我刚刚开始用 python 编程 代码 man = input ("what's your name mister") print("his name is "+man) 运行程序后收到
我在文件中创建一个类。声明一些类变量 A = 5 和另一个类变量 B = A+1。从另一个文件导入该类时,出现 NameError: name 'A' is not Defined。有没有办法解决这个
我一直在为 Minecraft 的 Raspberry Pi 版本制作 mod,每次在程序中输入其中一个命令时,我都会遇到非常令人沮丧的错误。这是我的代码: import minecraft.mine
为什么会这样: def fn(proc, *args, **kwargs): cache = proc.cache = {} def cached_execution(cache, *
我是 Python 的新手,正在学习类,正在尝试编写一个“个人信息”程序: 这是我的代码: class PersonalInfo(): def names(self, name):
我今天用这个简单的代码遇到了一个非常奇怪的问题: var1 = 1 var2 = 2 if var1 > var2 > var3: print('Does not run') print('D
我正在 try catch 无法加载模块时发生的任何异常。当前的结果是“except” block 没有被执行。 import sys def loadModule(module): try:
这是我的代码: import os if os.path.exists(r'C:\Genisis_AI'): print("Main File path exists! Continuing
我正在学习列表理解,通过切换变量我得到了 2 个不同的结果,尽管它们看起来应该工作相同。 数组 a 等于 [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0
我是一名优秀的程序员,十分优秀!