How to solve "TypeError: list indices must be integers".

顏灝 princismo at gmail.com
Mon Jan 21 23:33:30 EST 2008


On 1月22日, 上午3時05分, John Machin <sjmac... at lexicon.net> wrote:
> On Jan 22, 3:15 am, "顏灝" <princi... at gmail.com> wrote:
>
> > This is more details about my problem, which I running my py script
> > for my project. Programming in pythoncard that we can develop a GUI
> > based application easily.
>
> > I was assigned dialog.colorDialog(self) return value to a result
> > object, but I suspect that result.color is the attribute of the result
> > object that can assign to a string variable.
>
> The concepts "<developer> assigned <expression> to a <type> object"
> and "<expression> can be assigned to a <type> variable" just don't
> exist in Python.
>
> # Initially "name1" isn't bound to anything
> name1 = 42
> # "name1" now refers to an int object whose value is 42
> name1 = 'abc'
> # "name1" now refers to a str object whose value is 'abc'
> name2 = name1
> # "name2" now refers to the same str object
>
> What is "dialog.colorDialog(self) return value"? What is "a result
>  object"? Show us the code!
>
> What is the connection between your last sentence above and the
> "TypeError: list indices must be integers" problem? Show us the code!
>
>
>
> > There is a error prompt from python console "TypeError: list indices
> > must be integers".
> > Have any suggestion to solve this problem?
>
> Communication would be much easier if you show us the line of code
> that causes the error message.
>
> Here are two simple examples of what can trigger that error message:
>
> >>> a_list = [1, 42, 666]
> >>> not_an_integer = None
> >>> a_list[not_an_integer] = 9876
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers
>
> >>> a_name = a_list[not_an_integer]
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers
>
> Look for the pattern a_list[not_an_integer] in the statement that
> triggers the exception.
>
>
>
> > When I print  result.color, it is print out something like (255,0,0).
>
> Yes, that's most likely a tuple of (red, green, blue) values ... I'm
> not astonished; are you?
>
> > How to covert result.color into a string?
>
> How? Use elementary Python functionality, after you've decided what
> string representation you want. Examples:
>
> >>> color = (255, 128, 0)
> >>> "red=%d green=%d blue=%d" % color
>
> 'red=255 green=128 blue=0'>>> '.someclass {background-color: #%02x%02x%02x; }' % color
>
> '.someclass {background-color: #ff8000; }'
>
>
>
> > How to convert a string to
> > result.color type?
>
> Reverse the process.
>
> Again, what is the connection between "result.color" and the
> "TypeError: list indices must be integers" problem?

Many thanks, John Machin for your fast reply!
Regards,
Andreas



More information about the Python-list mailing list