472,102 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

indices question

pretty self-explanatory, here's what I put in:

while stat == 0 :
pgrid#ignore, A pre-defined function
print "what cell do you want?"
varcc = raw_input
grid[varc] = 'O'

And here's what I get back:

Traceback (most recent call last):
File "C:\py_prog\Tic Tac Toe.py", line 27, in <module>
grid[varc] = 'O'
TypeError: list indices must be integers

Please don't tell me that "list indices must be integers" because I know
that, Why can't I put a varible thats an integer instead?

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Sep 5 '08 #1
3 1001
Lanny wrote:
Please don't tell me that "list indices must be integers" because I know
that, Why can't I put a varible thats an integer instead?

raw_input() always returns a string. You have to convert the string into
an integer using int().

Christian

Sep 5 '08 #2
Lanny:
...
varcc = raw_input
grid[varc] = 'O'
...
Why can't I put a varible thats an integer instead?
'varcc' and 'varc' are different names.

'raw_input' isn't a function call, you may want to turn that into a
function call.

varc isn't an integer, you may have to convert it to integer first,
using int(), because Python lists aren't like Lua ones, they are
represented with a sequence of cells, and an integer number is used to
denote what cells you want. Note that the cell must already exists
before accessing its contents.

Maybe you want to use an associative array instead, named 'dict' in
Python, that allows you freedom in the type of the keys and allows you
to create cells on the fly.

Surely my answer isn't enough to solve your problems, but it may give
you a starting point.

Bye,
bearophile
Sep 6 '08 #3
Lanny:
>...
varcc = raw_input
grid[varc] = 'O'
...
Why can't I put a varible thats an integer instead?

'varcc' and 'varc' are different names.

'raw_input' isn't a function call, you may want to turn that into a
function call.

varc isn't an integer, you may have to convert it to integer first,
using int(), because Python lists aren't like Lua ones, they are
represented with a sequence of cells, and an integer number is used to
denote what cells you want. Note that the cell must already exists
before accessing its contents.

Maybe you want to use an associative array instead, named 'dict' in
Python, that allows you freedom in the type of the keys and allows you
to create cells on the fly.

Surely my answer isn't enough to solve your problems, but it may give
you a starting point.

Bye,
bearophile
Thanks, this was very usefull

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Sep 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by dan | last post: by
5 posts views Thread by Ross MacGregor | last post: by
7 posts views Thread by Bart Torbert | last post: by
9 posts views Thread by Richard A. DeVenezia | last post: by
9 posts views Thread by Randell D. | last post: by
3 posts views Thread by David Mathog | last post: by
3 posts views Thread by GavinCrooks | last post: by
2 posts views Thread by smichr | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.