Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python IndexError Problem
New Posts  All Forums:Forum Nav:

Python IndexError Problem

post #1 of 3
Thread Starter 
So I'm working on a homework 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']
Code:
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.
Code:
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
Thorindia
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5-750 ASUS P7P55D-E SAPPHIRE Radeon HD 5830 G.Skill Ripjaws Series 4GB (2x2GB) DDR3 1600 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F3 1TB DVD-RW Windows 7 Home Premium 64-Bit 21" 
KeyboardPowerCaseMouse
Logitech G15 Corsair HX 650 CM 690 Logitech G5 
  hide details  
Reply
Thorindia
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5-750 ASUS P7P55D-E SAPPHIRE Radeon HD 5830 G.Skill Ripjaws Series 4GB (2x2GB) DDR3 1600 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F3 1TB DVD-RW Windows 7 Home Premium 64-Bit 21" 
KeyboardPowerCaseMouse
Logitech G15 Corsair HX 650 CM 690 Logitech G5 
  hide details  
Reply
post #2 of 3
If your list length is 10, the last iteration of that while loop will have i equal to 9. It will then try to access bookList[i], bookList[i+1], and bookList[i+2], or bookList[9], bookList[10], and bookList[11]. [10] and [11] are out of bounds
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #3 of 3
Thread Starter 
Thank you for your input. I figured out what was wrong.
Thorindia
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5-750 ASUS P7P55D-E SAPPHIRE Radeon HD 5830 G.Skill Ripjaws Series 4GB (2x2GB) DDR3 1600 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F3 1TB DVD-RW Windows 7 Home Premium 64-Bit 21" 
KeyboardPowerCaseMouse
Logitech G15 Corsair HX 650 CM 690 Logitech G5 
  hide details  
Reply
Thorindia
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5-750 ASUS P7P55D-E SAPPHIRE Radeon HD 5830 G.Skill Ripjaws Series 4GB (2x2GB) DDR3 1600 
Hard DriveOptical DriveOSMonitor
Samsung Spinpoint F3 1TB DVD-RW Windows 7 Home Premium 64-Bit 21" 
KeyboardPowerCaseMouse
Logitech G15 Corsair HX 650 CM 690 Logitech G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python IndexError Problem