473,383 Members | 1,788 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,383 software developers and data experts.

IndexError: list index out of range

This is my first post, so please be patient with me.. So i've been coding in python for about a month, and i'm not exactly brilliant, so i'm stumped. The system is a basic login (A-Level work) for a "merit system", however the list cannot get past the first user in the class?

Code:
Expand|Select|Wrap|Line Numbers
  1. def findStudent(username):
  2.     for i in range(len(students)):
  3.         if students[i][0] == username:
  4.             return i
  5.     return -1
Sorry if i've been vague, feel free to ask any further questions

thanks in advance,
Ed
Dec 16 '11 #1
4 1722
Okay copy/paste messed up the indents, try #2:

def findStudent(username):
____for i in range(1,len(students)):
________if students[i][0] == username:
________return i
____return -1
Dec 16 '11 #2
bvdet
2,851 Expert Mod 2GB
Ed,

Indents display properly when you use code tags.

What is students? From your code I would expect it to be a list of lists. If you use range(1, len(students)), you will always skip the first element (the first element of a list is always at index 0). If students is empty, you will get an IndexError.
Dec 16 '11 #3
Glenton
391 Expert 256MB
Can you post the error and perhaps a simple example of the code failing.
Dec 18 '11 #4
Glenton
391 Expert 256MB
Good point BVDET.
If you add in a line like this:
Expand|Select|Wrap|Line Numbers
  1. if len(student[i])==0: continue
after the for statement and before the if statement you'll be able to skip empty records. Of course it would be better not to have empty records but it depends how things are set up.

Another thing that is quite common is to set up a class object for student instead of a list. A list is unwieldy because you need to keep track of what each entry corresponds to and is therefore prone to error. A student object could have all the variables, and could also be printed using special methods.
Dec 19 '11 #5

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

Similar topics

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. ...
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):
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...
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...
1
by: Karen Rosen | last post by:
This is my first program of any sort I am getting this error File "karenrosen5.py", line 162, in <module> record.append(triad("NoReleve",line)) File "karenrosen5.py", line 115, in triad...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.