2

So I'm working on an assignment, and I've come to a obstacle I can't seem to overcome. I'm essentially designing a rudimentary Library System. I have to read in from a file the books, librarians, and patrons and parse them in to lists then to objects. I've gotten the parsing to lists part down and have them ready to be used to create objects. Here's where I have the issue when I try to read from the lists I get and IndexError, and I can't figure out how to fix it.

My list where the first item is the ISBN, then the Title, then Author, then it repeats, i.e. ['123','The Scarlet Letter', 'Nathaniel Hawthorne']

self._bookList = ['123', 'The Scarlet Letter', 'Nathaniel Hawthorne', '456', 'Lord of the Rings', 'JRR Tolkien', '789', 'A Tale of Two Cities', 'Charles Dickens', '10112', 'Wuthering Heghts', 'Emily Bronte', '131415', 'Jane Eyre', 'Charlotte Bronte', '161718', 'Pride and Predudice', 'Jane Austin']

Snippet where I have the problem. I've tried using len(self._bookList)-1, +1, +2, +3. Nothing seems to work.

def _addToCollection(self):
    i = 0
    while i < len(self._bookList):
        ISBN = []
        ISBN.append(self._bookList[i])
        title = []
        title.append(self._bookList[i+1])
        author = []
        author.append(self._bookList[i+2])      
        #print(self._bookList[i],self._bookList[i+1],self._bookList[i+2])
        BookCollection.addBook(self,str(ISBN[i]),str(title[i]),str(author[i]))
        i += 1

2 Answers 2

2

The problem is that you are assigning empty lists during every iteration. When you call ISBN[i] the list always has only one element. Therefore it fails as soon as i becomes larger than zero.

One way to fix it is to notice that you don't need the lists at all:

def _addToCollection(self):
    i = 0
    while i < len(self._bookList):
        ISBN = self._bookList[i]
        title = self._bookList[i+1]
        author = self._bookList[i+2]
        BookCollection.addBook(self,str(ISBN),str(title),str(author))
        i += 3

I'd suggest instead that you take a look at the grouper recipe from the itertools documentation as this seems to do exactly what you need:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

You can use it like this:

for ISBN, title, author in grouper(3, self._bookList):
    BookCollection.addBook(self, str(ISBN), str(title), str(author))
Sign up to request clarification or add additional context in comments.

8 Comments

When I add the three holding lists to the beginning like so. It goes into an infinite loop def _addToCollection(self): i = 0 ISBN = [] title = [] author = [] while i < len(self._bookList): ISBN.append(self._bookList[i]) title.append(self._bookList[i+1]) author.append(self._bookList[i+2]) BookCollection.addBook(self,str(ISBN[i]),str(title[i]),str(author[i])) i += 1
You don't need to use lists at all.
I see how it works, but now I'm getting an NameError. def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) def _addToCollection(self): for ISBN, title, author in grouper(3, self._bookList): BookCollection.addBook(self, str(ISBN), str(title), str(author))
@Mark Byers: So I need to essentially dissect whats functions are used from grouper according to the documentation and also implement those?
@Mark Byers: I used from itertools import * and I'm still getting a NameError. Am I forgetting something?
|
0

Change your init method and just do this

a = BookCollection.addBook(*self._bookList)

2 Comments

How would that help? I'm confused by your response
Rather than do a complicated construction method just unpack the contents of the list straight into addBook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.