473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Error - list index out of range

2
Hi all,

I'm just starting my way into Python and I'm trying to use a while loop with lists but keep getting the list index out of range error.
(Please ignore any other foolish mistakes I might find later on J):

Expand|Select|Wrap|Line Numbers
  1.   DBReportsArray = [6000,5000,4000,3000,2000,1000,100]         
  2.   curNum = 0 
  3.   RepInDB = tssh.command ("ls -l /something | wc -l")[0] ##something to get a result by ssh
  4.   RepInDB = [y.replace("\n","") for y in RepInDB] ##replaces a non-good format
  5.   RepInDB = RepInDB[0]
  6.   print RepInDB ##I got 351
  7.   print self.DBReportsArray[curNum] 
  8.   while self.DBReportsArray[curNum] <= RepInDB: 
  9.      curNum+=1
  10.   RepToChange = RepInDB-self.DBReportsArray[self.curNum]
  11.   print RepToChange
  12.  
Thanks in advance,
Einat
Aug 24 '10 #1
2 4424
bvdet
2,851 Expert Mod 2GB
Einat,

Either RepInDb is an empty list, or the value of curNum is equal to or exceeds the length of the list self.DBReportsArray as in:
Expand|Select|Wrap|Line Numbers
  1. >>> [][0]
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in ?
  4. IndexError: list index out of range
  5. >>> alist = [0,1,2,3,4,5]
  6. >>> alist[0]
  7. 0
  8. >>> alist[5]
  9. 5
  10. >>> alist[6]
  11. Traceback (most recent call last):
  12.   File "<interactive input>", line 1, in ?
  13. IndexError: list index out of range
  14. >>> 
Aug 24 '10 #2
Einat
2
Appreciate your reply.

I tried verifying the content of RepInDb and curNum and both were in order.
Anyway I converted the "while" loop to a "for" loop and now it works:
Expand|Select|Wrap|Line Numbers
  1. DBReportsArray = [6000,5000,4000,3000,2000,1000,100]         
  2. curNum = 0 
  3. RepInDB = tssh.command ("ls -l /something | wc -l")[0] ##something to get a result by ssh
  4. RepInDB = [y.replace("\n","") for y in RepInDB] ##replaces a non-good format
  5. RepInDB = RepInDB[0]
  6. print RepInDB
  7. for num in range(len(self.DBReportsArray)):
  8.     if self.DBReportsArray[num] <= RepInDB:
  9.        break
  10.     return num,RepInDB 
  11.  
Thanks again,
E
Aug 29 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
5
by: questions? | last post by:
I want to do list index function. >>> y= >>> y >>> y.index Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsubscriptable object It works with y=. Anyone has...
8
by: sam | last post by:
hey everybody, this is my first time posting here. i'm pretty new to python and programming in general (as you'll soon work out for yourselves...) i'm trying to code a version of a selection...
35
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
11
by: Steve | last post by:
I'm trying to create a list range of floats and running into problems. I've been trying something like: a = 0.0 b = 10.0 flts = range(a, b) fltlst.append(flts)
7
by: python101 | last post by:
My code s= for k in range(len(s)): st=s.count(s) if st>=2: print s.pop(s.index(s)) Traceback (most recent call last):
1
by: ApoorvaDesai | last post by:
How do I check if a certain column exists in the CSV file, from the following code I try to check if s exists. It gives me an error INDEX ERROR: List index out of range. If its Null I expect it to...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
4
by: Derbylad | last post by:
After starting my coursework I've come across the following error code... "List index out of range". I was expecting this to be something very simple that I've done wrong and still imagine it will be...
2
by: Chriskim | last post by:
I wrote a program that uses PyQt4, mathplotlib to generates graphs. I made a executable file using Py2exe. Everything is working fine, but when I exit from my program, a window message pops...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.