I am writing some simple game in Python 3.4. I am totally new in Python. Code below:
def shapeAt(self, x, y): return self.board[(y * Board.BoardWidth) + x] Throws an error:
TypeError: list indices must be integers, not float For now I have found that this may happen when Python "thinks" that list argument is not an integer. Do you have any idea how to fix that?
45 Answers
int((y * Board.BoardWidth) + x) use int to get nearest integer towards zero.
def shapeAt(self, x, y): return self.board[int((y * Board.BoardWidth) + x)] # will give you floor value. and to get floor value use math.floor(by help of m.wasowski)
math.floor((y * Board.BoardWidth) + x) 3If x, y are numbers or strings representing number literals you can use int to cast to integer, while floating point values get floored:
>>> x = 1.5 >>> type(x) <type 'float'> >>> int(x) 1 >>> type(int(x)) <type 'int'> This is probably because your indices are of type float where these should be ints (because you are using them as array indices). I wouldn't use int(x), I think you probably intended to pass an int (if not, use return self.board[(int(y) * Board.BoardWidth) + int(x)] of course).
You may also want to get floor value to get your index and here is how to do it:
import math def shapeAt(self, x, y): return self.board[math.floor((y * Board.BoardWidth) + x)] You can also use Python's type() function to identify type of your variables.
what is the type of x and y you need to check that, then convert them to integer type using int:
def shapeAt(self, x, y): return self.board[(int(y) * Board.BoardWidth) + int(x)] if you want to first store them:
def shapeAt(self, x, y): x,y = int(x),int(y) return self.board[(y * Board.BoardWidth) + x] 4Basically, you just call a int() builtin:
def shapeAt(self, x, y): return self.board[int((y * Board.BoardWidth) + x)) However, if you want to use it to anything more than practise or dirty script for you, you should think of handling edge cases. What if you made mistake somewhere and put weird values as arguments?
The more robust solution would be:
def shapeAt(self, x, y): try: calculated = int((y * Board.BoardWidth) + x) # optionally, you may check if index is non-negative if calculated < 0: raise ValueError('Non-negative index expected, got ' + repr(calculated)) return self.board[calculated] # you may expect exception when converting to int # or when index is out of bounds of your sequence except (ValueError, IndexError) as err: print('error in shapeAt:', err) # handle special case here # ... # None will be returned here anyway, if you won't return anything # this is just for readability: return None If you are beginner, you might be suprising, but in Python negative indexes are perfectly valid, but they have special meanings. You should read about it, and decide if you want to allow them in your function (in my example they are disallowed).
You may also want to read about rules of converting to int:
and consider, if for you it would not be better to user floor or ceiling, before you try to cast to int:
Just make sure, you have a float before calling those! ;)