Jump to content
  • Advertisement
Sign in to follow this  
Tavyn

Python Help

This topic is 3670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code:
def listItems(self):
        for i in self.items:
            print "("+str(i)+") "+self.items.name
Gives this error: TypeError: object cannot be interpreted as an index This is part of an inventory class. It lets me put objects in the list, but won't let me display them apparently. I'm still really new to python, and programming in general, so it's probably something basic, but I can't seem to find anything helpful with a google search. Could someone please point out what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Tavyn
This code:
def listItems(self):
for i in self.items:
print "("+str(i)+") "+self.items.name

Gives this error:
TypeError: object cannot be interpreted as an index
A python for loop iterates over objects directly - *not* indices. You probably meant to emulate a C-style for loop, which would be done something like this:
def listItems(self):
for i in range(len(self.items)):
print "("+str(i)+") "+self.items.name

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Alternately:
def listItems(self):
for i in self.items:
print "(" + str(i) + ") " + i.name
Except that you do not print the index before the name, instead you print the object i according to i's __str__ method (which is probably the default implementation, thus printing the class description).

Share this post


Link to post
Share on other sites
I assume Oluseyi intended to write this.
def listItems(self):
for i, e in enumerate(self.items):
print "(" + str(i) + ") " + e.name


Share this post


Link to post
Share on other sites
Quote:
Original post by gumpy
I assume Oluseyi intended to write this.
def listItems(self):
for i, e in enumerate(self.items):
print "(" + str(i) + ") " + e.name


Yep.

Kids, don't use indices to iterate over a list (except where you really have to). It's brain damage inflicted by lower-level languages like C and C++. You don't have to *think about the numbers* 1 through 12 (excuse me, 0 through 11) to crack a dozen eggs into a bowl. You just keep cracking eggs until the carton is empty. It also doesn't work (or at least, requires significant setup) for certain kinds of sequences, such as files:


f = file('foo.txt')

# Python wants you to do this:
for i, line in enumerate(f):
print (i + 1), line # Each line is read on demand and each number is generated on demand.

# To do it the C way, you would have to prepare ahead of time:
text = f.read().split('\n')
for i in range(len(text)):
print (i + 1), text
# Ewww! You had to hold the whole file in memory (and do a bunch of
# string-splitting work) and generate a corresponding list of numbers,
# just so that you could use the numbers to retrieve each line of the
# file in order again. Round-about much?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!