zak100 Posted October 8, 2020 Posted October 8, 2020 Hi, I have written code, I am presenting a portion of it. I want to modify my original code for inputting, so I have created this portion for testing. My code is: import numpy as np class Board: #Matrix = np.empty((8, 8)) #Matrix.fill(0) #create a 8 * 8 matrix, each entry is -1, indicating no activity on the board Matrix = np.array( [[-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, -1, -1]]) def __init__(self, n): # runs from 0 to n-1. So there is no need for subtracting -1 from the range argument. # str(INT) converts integer to string self.n=n def findComputerMove(self, row, column, pieceStr): print("Inside find Computer Move") def computerModule(self, pieceStr): n = 8 row = -1 column = -1 pName = "" row, column, pName = self.findComputerMove(row, column, pieceStr) return True def main(self): n = 8 row = -1 column = -1 pName = "" moveCount = 0 # self.display(n) # self.displayBDMatrix(n) #self.createInitialBoard(n) userMove = True compMove = True draw = False while (userMove and compMove and not draw): if (moveCount % 2 == 0): pieceStr = "Y" compMove = self.computerModule(pieceStr) print("compMove=", compMove) else: pieceStr = "X" userMove, draw = self.userModeule(pieceStr, draw) moveCount = moveCount + 1 if (draw): print("Game drawn, nobody wins") return if (userMove == True): print("User wins the game") else: print("Computer wins the game") if __name__ == "__main__": objBoard = Board(8) objBoard.main() I am getting following error message: Quote Traceback (most recent call last): File "/home/PycharmProjects/AIHW/nonTypeObjErr.py", line 67, in <module> objBoard.main() File "/home/PycharmProjects/AIHW/nonTypeObjErr.py", line 39, in main compMove = self.computerModule(pieceStr) File "/home/PycharmProjects/AIHW/nonTypeObjErr.py", line 21, in computerModule row, column, pName = self.findComputerMove(row, column, pieceStr) TypeError: 'NoneType' object is not iterable Inside find Computer Move Somebody please guide me. Zulfi.
Ghideon Posted October 8, 2020 Posted October 8, 2020 (edited) 1 hour ago, zak100 said: Somebody please guide me. Ok! First step: explain, in detail, what steps you have taken so far in fining out what went wrong. From there we will move on to next step. (There are obvious issues but you are asking for guidance, so I try to guide rather than just pointing out the error) Edited October 8, 2020 by Ghideon 1
FragmentedCurve Posted October 8, 2020 Posted October 8, 2020 import numpy as np class Board: # ... def findComputerMove(self, row, column, pieceStr): print("Inside find Computer Move") def computerModule(self, pieceStr): n = 8 row = -1 column = -1 pName = "" row, column, pName = self.findComputerMove(row, column, pieceStr) # This is the problem return True def main(self): # ... if __name__ == "__main__": objBoard = Board(8) objBoard.main() In computerModule you're calling findComputerMove. What does findComputerMove return? (Your code has more problems after you answer that and solve the problem.) 1
zak100 Posted October 8, 2020 Author Posted October 8, 2020 Hi, Thanks for your response. Ghideon: I have written more than 700 lines of code but now I want to do testing of the board so I created this small code. In my original program, I am not getting this error. I can't understand what to do? I would integrate this code with my original program but now I am stuck. FragmentedCurve: findComputer(....) is returning 3 values and this code is working fine in original program. Please guide me. Please guide me. Zulfi.
FragmentedCurve Posted October 8, 2020 Posted October 8, 2020 (edited) In your OP, findComputerMove is defined as: def findComputerMove(self, row, column, pieceStr): print("Inside find Computer Move") I don't see 3 values being returned. I don't see anything being returned. When you write a line of code like the following: a, b, c = self.findComputerMove(foo, bar, something) the assumption is that findComputerMove returns a tuple of length 3. Is that clear? Edited October 8, 2020 by FragmentedCurve Accidentally deleted a sentence before submitting 1
zak100 Posted October 8, 2020 Author Posted October 8, 2020 Okay. Good response. I would check Zulfi Hi, Thanks a lot, I solved the problem. God blesses you. Zulfi.
Ghideon Posted October 8, 2020 Posted October 8, 2020 (edited) 25 minutes ago, zak100 said: Thanks a lot, I solved the problem. Good to hear that. Maybe you could share the solution in case other members are interested? Edited October 8, 2020 by Ghideon Spelling 1
zak100 Posted October 8, 2020 Author Posted October 8, 2020 (edited) Hi, findComputerMove(....): Above function is not returning values, Thanks a lot. To any one of my classmates: This is my assignment, if anybody copies my logic, he is the copier, I am not because I am using my name and logic is mine. Zulfi. Edited October 8, 2020 by zak100
Ghideon Posted October 8, 2020 Posted October 8, 2020 9 minutes ago, zak100 said: To any one of my classmates: This is my assignment, Ok. Then you should post in homework help.
FragmentedCurve Posted October 8, 2020 Posted October 8, 2020 (edited) 5 hours ago, zak100 said: Hi, findComputerMove(....): Above function is not returning values, Thanks a lot. To any one of my classmates: This is my assignment, if anybody copies my logic, he is the copier, I am not because I am using my name and logic is mine. Zulfi. Let's be a little more rigorous. It's not entirely accurate to say findComputerMove is not returning values. The problem is it's not returning a tuple and is instead returning the None type. Python is strongly typed. Let's go back and rewrite a previous code snippet: # Original code # a, b, c = self.findComputerMove(foo, bar, something) result = self.findComputerMove(foo, bar, something) a, b, c = result The first assignment is just a normal variable value assignment. At this moment, Python doesn't assert that the type returned by findComputerMove be a specific type. It will accept an integer, float, string, and even a tuple. Whatever type that findComputerMove returns, defines the result's type. However, the second assignment isn't the same as the first. It's a tuple assignment. The = symbol is a tuple assignment and it expects the right side to be a type that is iterable. And it must be the same length as the left. It's short for the following: result = self.findComputerMove(foo, bar, something) a = result[0] b = result[1] c = result[2] Here's some code about types for you to look over and think about: >>> def do_nothing(): ... pass ... >>> foo = do_nothing() >>> bar = None >>> type(foo) <class 'NoneType'> >>> type(bar) <class 'NoneType'> >>> foo is bar True >>> foo is None True >>> foo == bar True >>> foo == None True >>> def do_none(): ... return None ... >>> do_none() is do_nothing() True >>> do_none() == do_nothing() True >>> Edited October 8, 2020 by FragmentedCurve 1
zak100 Posted October 9, 2020 Author Posted October 9, 2020 Hi, I am getting the same error at other place. This time I am using the "return" statement also. The error occurs at this statement. result = [] result = self.checkTheBelow_Squeeze(curr_row, col) resOfChkBelowSqueeze, cntOfBelowSqueezed = result The complete function code is given below: def checkTheBelow_Squeeze(self, curr_row, col): print("CheckTheBelow Squeeze 1:Entered the function") if (curr_row >= 2): found = False opp_piece_found = False for row in range(curr_row-1,-1, -1): #loop will run downwards to 0 not -1 if (Board.Matrix[row][col] == -1): # next above location is empty so no squeeze possible print("CheckTheBelow Squeeze 2:Not possible b/c space found") found = False cntOfBelowSqueezed = 0 break elif (Board.Matrix[row][col] >= 0 and Board.Matrix[row][col] <= 7): # opp_piece_found if (row == 0): # we have reached the bottom row but comp piece not found only sequeces of opp piece, so no squeeze print("CheckTheBelow Squeeze 3:Not possible b/c row zero reached") found = False cntOfBelowSqueezed = 0 break else: if (opp_piece_found == False): # current element is opp_piece and we have not reached the end row, so squeeze maybe found opp_piece_found = True cntOfBelowSqueezed = 1 else: if (cntOfBelowSqueezed < 7): #Another opponet piece found cntOfBelowSqueezed = cntOfBelowSqueezed + 1 print("CheckTheBelow Squeeze 4:Another opp piece found, count =" + str(cntOfBelowSqueezed)) else: print("This is trouble: cnt is more than 7") elif (Board.Matrix[curr_row][col] >= 8 and Board.Matrix[curr_row][col] <= 15): if (opp_piece_found == True): found = True print("CheckTheBelow Squeeze 5:Computer piece found, squeeze successful, count =" + str(cntOfBelowSqueezed)) break; else: print("CheckTheBelow Squeeze 6:Not possible b/c computer piece found before opp piece") found = False cntOfBelowSqueezed = 0 break if (cntOfBelowSqueezed==0): return False, cntOfBelowSqueezed else: return True, cntOfBelowSqueezed Somebody please guide me. Zulfi. Hi, I found the error, problem was again the return statement. I was using "if condition" and return statement was inside "if" but "if condition' got false so return did not execute
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now