I'm trying to add a player to a board in python, but am struggling with the implmentation.
The player object's attributes gets validated when a player is initiated via the operator
library. I want to show the exceptions as they occur; ie. put the initiation of each player in a try-except.
Problem is that when I do it as I am bellow I get an error saying:
Traceback (most recent call last): File "./main.py", line 88, in move r.forward() AttributeError: 'NoneType' object has no attribute 'forward'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "./main.py", line 161, in main() File "./main.py", line 141, in main move(choice, player) # Peform the moves on the player File "./main.py", line 91, in move Try again!\n>>> ".format(err, r.initial[0], r.initial[1], r.initial[2])) AttributeError: 'NoneType' object has no attribute 'initial'
The operations mentioned above are movements handled by the player class (player.py
). It's clear that I am somehow returning a NoneType, but I'm not sure why this is occuring.
It happens when I have entered an incorrect player-position during initiation and then add a correct one.
So basically this is what I do:
- Initiate the board
- Initiate a player outside of bounds (or on another player)
- Initiate a correct player position.
- Error occurs.
However, there are no errors if I add players correctly (ie. step 3 doesn't occur).
def add_player(board):
try:
choice = input("Please enter the current player's initial position.\nRemember to keep inside board's limits!\n>>> ").split()
if len(choice) == 3 and validate_player(choice[0], choice[1], choice[2]): # Check the length of supplied input and check type-integrity
return Player(int(choice[0]), int(choice[1]), choice[2], board) # Initiate a player with the supplied input. Each player is assigned to a Board (many-to-one relation).
except Exception as err:
print(err)
add_player(board)