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
6
  • Why the self? Are these members of a class? Commented Mar 18, 2018 at 1:57
  • @StephenRauch yes the functions are, ive just cut them down so there isnt alot to read through. Commented Mar 18, 2018 at 1:58
  • Well you cut them down to the point of not being able to answer. You can learn How to Ask a good question and create a Minimal, Complete, and Verifiable example. That makes it easier for us to help you. Commented Mar 18, 2018 at 1:59
  • @StephenRauch ive updated the code Commented Mar 18, 2018 at 2:01
  • Where is the class definition? You added a bunch of stuff that is not needed to reproduce the problem. Please read through the links I commented earlier. Commented Mar 18, 2018 at 2:02

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

says im missing a positional argument "parent", when i add parent in, it says that it is not defined.
Great @sharjeel, that means we are getting closer to the solution. Can you update the question to include how exactly you add parent in?
@sharjeel Call the class like this: root = tk.Tk() and then c = ChessBoard(root). Let me know if this helps.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.