indices question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lanny

    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 news@netfront.n et --
  • Christian Heimes

    #2
    Re: indices question

    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

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: indices question

      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

      Comment

      • Lanny

        #4
        Re: indices question

        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 news@netfront.n et --

        Comment

        Working...