Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


princismo at gmail

Jan 21, 2008, 8:15 AM

Post #1 of 4 (45522 views)
Permalink
How to solve "TypeError: list indices must be integers".

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.

There is a error prompt from python console "TypeError: list indices
must be integers".
Have any suggestion to solve this problem?

When I print result.color, it is print out something like (255,0,0).
How to covert result.color into a string? How to convert a string to
result.color type?

adrian
--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Jan 21, 2008, 11:05 AM

Post #2 of 4 (45459 views)
Permalink
Re: How to solve "TypeError: list indices must be integers". [In reply to]

On Jan 22, 3:15 am, "" <princi...@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?


princismo at gmail

Jan 21, 2008, 8:33 PM

Post #3 of 4 (45441 views)
Permalink
Re: How to solve "TypeError: list indices must be integers". [In reply to]

On 122, W305, John Machin <sjmac...@lexicon.net> wrote:
> On Jan 22, 3:15 am, "C" <princi...@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
--
http://mail.python.org/mailman/listinfo/python-list


princismo at gmail

Jan 21, 2008, 8:34 PM

Post #4 of 4 (45439 views)
Permalink
Re: How to solve "TypeError: list indices must be integers". [In reply to]

On 1月22日, 上午1時56分, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Mon, 21 Jan 2008 08:15:02 -0800 (PST), "ÃCø¯" <princi...@gmail.com>
> declaimed the following in comp.lang.python:
>
> > 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.
>
>         Showing the actual code would help...
>
> > There is a error prompt from python console "TypeError: list indices
> > must be integers".
> > Have any suggestion to solve this problem?
>
>         Make sure you only have an integer when subscripting into a list?
>
> > When I print  result.color, it is print out something like (255,0,0).
>
>         Looks like a tuple -- probably
>                 (red-level, green-level, blue-level)
> where *-level is in the range 0..255 (x00..xFF); your example would be
> (full red, no green, no blue)
>
> > How to covert result.color into a string?  How to convert a string to
> > result.color type?
>
>         What type of string?
>                 "0x%2.2X%2.2X%2.2X" % (r, g, b)
> will produce a hex string of the form
>                 0xFF0000
> given your sample RGB
>
>         If you want to get "Red", you'll need a look-up table and probably
> some least-distance error function for items that don't directly match.
>
> (255, 0, 0)             Red
> (255, 255, 0)   Yellow
> (255, 0, 255)   Magenta
> (0, 255, 0)             Green
> (0, 255, 255)   Cyan
> (0, 0, 255)             Blue
> (0, 0, 0)               Black
> (255, 255, 255) White
>
>         But where would you put:        (127, 127, 63)          [A half-black Yellow
> with 1/4 blue added].. Is it closer to Yellow, or to Black: 255-127 =>
> 128, but 127 - 0 => 127... Shorter simplistic distance means map this to
> (0, 0, 0)... But (128, 128, 64) simplistic shorter distance would map to
> Yellow
>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr...@ix.netcom.com                wulfr...@bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a...@bestiaria.com)
>                 HTTP://www.bestiaria.com/

Many thanks, Dennis Lee Bieber for your fast reply!
Regards,
Andreas
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.