[英]Function not defined error when being called
so im returning two inputs and want to use them within another function, when i run the code, it says the function im returning the code from is not defined. 所以我返回两个输入并想在另一个函数中使用它们,当我运行代码时,它说返回代码的函数未定义。 any idea what the problem may be?
知道可能是什么问题吗?
Chessboard Class: 棋盘类:
class ChessBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=70, color1="white", color2="lightgrey"):
self.rows = rows
self.columns = columns
self.size = size
self.color1 = color1
self.color2 = color2
self.pieces = {}
The function that is returning two inputs: 返回两个输入的函数:
def UserInput(self): #Tester Function
count = 0
while count < 2:
KingRow = int(input("Choose Row: ")) #mighht not be needed
KingColumn = int(input("Choose Column: ")) #choose the column
return KingRow, KingColumn
count = count + 1
The function i would like to use it within: 我想在其中使用的功能:
def KingMoves(self, rows, columns):
FinalMove = []
c = ChessBoard(parent)
KingRow, KingColumn = c.UserInput()
FinalMove.append(((KingRow - 1),(KingColumn)))
FinalMove.append(((KingRow + 1),(KingColumn)))
FinalMove.append(((KingRow),(KingColumn + 1)))
FinalMove.append(((KingRow + 1),(KingColumn + 1)))
FinalMove.append(((KingRow - 1),(KingColumn + 1)))
FinalMove.append(((KingRow + 1),(KingColumn - 1)))
FinalMove.append(((KingRow - 1),(KingColumn - 1)))
return FinalMove;
Current Error: 当前错误:
name 'UserInput' is not defined
Try this first: how to use a Python function with keyword “self” in arguments 首先尝试: 如何在参数中使用带有关键字“ self”的Python函数
If that doesn't work try this: 如果这样不起作用,请尝试以下操作:
Functions within a Python class are called methods. Python类中的函数称为方法。 They normally take a
self
parameter before their other parameters. 他们通常在其他参数之前先使用一个
self
变量。 Furthermore, methods cannot "see" each other directly; 此外,方法不能直接“看到”彼此; you need to call them as
self.method(args)
instead of just method(args)
. 您需要将它们称为
self.method(args)
而不是method(args)
。
See, this is how I call another functions within a class: 瞧,这就是我在一个类中调用另一个函数的方式:
def func1(self):
return "Whoop"
def func2(self):
whoop = self.func1()
return whoop
Also, try using a for
statement instead of a while
. 另外,请尝试使用
for
语句,而不要使用while
。 You don't have too, but it's less lines of code and easier. 您也没有,但是代码行更少,更容易。
def UserInput(self): #Tester Function
for x in range(0, 2):
KingRow = int(input("Choose Row: ")) #mighht not be needed
KingColumn = int(input("Choose Column: ")) #choose the column
return KingRow, KingColumn
Unless you invoke the ChessBoard
class, Python doesn't know where/what the UserInput
function is. 除非您调用
ChessBoard
类,否则Python将不知道UserInput
函数的位置/含义。 First invoke the class, and then call its function : 首先调用该类,然后调用其函数:
c = ChessBoard()
KingRow, KingColumn = c.UserInput()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.