0

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
sharjeel
  • 61
  • 9

2 Answers2

1

Try this first: how to use a Python function with keyword “self” in arguments

If that doesn't work try this:

Functions within a Python class are called methods. They normally take a self parameter before their other parameters. Furthermore, methods cannot "see" each other directly; you need to call them as self.method(args) instead of just method(args).

Source


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. 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
NekoTony
  • 54
  • 8
1

Unless you invoke the ChessBoard class, Python doesn't know where/what the UserInput function is. First invoke the class, and then call its function :

c = ChessBoard()
KingRow, KingColumn = c.UserInput()
FatihAkici
  • 4,679
  • 2
  • 31
  • 48
  • says im missing a positional argument "parent", when i add parent in, it says that it is not defined. – sharjeel Mar 18 '18 at 02:45
  • Great @sharjeel, that means we are getting closer to the solution. Can you update the question to include how exactly you add parent in? – FatihAkici Mar 18 '18 at 02:56
  • @sharjeel Call the class like this: root = tk.Tk() and then c = ChessBoard(root). Let me know if this helps. – FatihAkici Mar 18 '18 at 03:46