I'm so kind of new to python (and coding) and I just want to create a board (for a console game) based on the player desire.
Basically it's this...
import array
print("What size do you want the board?")
Boardsize = input()
Tablero = array('b' [Boardsize, Boardsize])
for w in Boardsize:
for h in Boardsize:
Boardsize(w)(h).append('.')
print (Tablero)
At least that's my idea, but the compiler say:
Tablero = array('b'[Boardsize, Boardsize])
TypeError: string indices must be integers
What's going on
input()
returns a string (the characters you typed in, e.g. "123"), but you are getting a TypeError
because you are passing a string to something that expects a number (e.g. 123, without the quotes).
Solution
The fix is convert the string to a number by passing it through the int(...)
constructor, e.g. int(input())
(just like int("12")
will give you 12
).
I'd like to apologize if you are not new to programming and this was a silly mistake, but in case you are new, here was my thought process which helped me debug the issue. I hope you do not find it condescending; I am sharing my thought process so others in similar situations can fix similar bugs.
How to diagnose these kinds of issues
You would debug this as follows by backtracking one step at a time:
� First test to make sure that you understand how to make array
s properly. I would for example try to make an array of size 3x3 to make sure I understood the API.
>>> array(..., [3,3])
<array object at 0x...>
� Okay, that worked! We seem to be able to make array
s properly if I just type in the numbers array(..., [3,3])
. Now let's try with input()
.
>>> boardsize = input()
>>> array(..., [boardsize, boardsize])
TypeError: string indices must be integers
� This is odd. I just made a 3x3 array with array(..., [3,3])
, why doesn't array(..., [boardsize, boardsize])
work? Let's check what the value of boardsize
really is:
>>> boardsize
'3'
� How odd, the value seems to be 3
, right? Let me double-check to make sure.
>>> boardsize == 3
False
� Wait, '3'!=3 ??? How is '3' not the same as 3?
>>> type(boardsize)
<class 'str'>
� Ahah! The '
I see mean it is a string. It must be the case that input
returns a string. This makes sense, since for example I could type in "cat" and make boardsize == 'cat'
, and I should not expect python to be able to tell whether an arbitrary string is a number.
>>> '3'
'3'
>>> 3
3
>>> boardsize
'3'
� The fix would be to google for python convert string to number
: second hit: "use the built-in int(...)
function
tl;dr: Work your way backwards towards the error, sanity-checking yourself at each step. When you start making large programs, you can use automatically-called sanity check functions and "unit tests" to make debugging easier.
(sidenote: If you are curious how objects are printed out, it comes from the special __repr__
method that all classes define. Calling repr(something)
will show fairly unambiguously what kind of object something
is; repr
is automatically called on the output of what you type into the interactive interpreter.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With