Python Forum
list index out of range
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list index out of range
#1
I am in the second half term of the first year of a GCSE Python course. For a piece of Homework, I need to write a program that converts marks to grades based on given grade boundaries, then store them in a list to print out the number and percentage of passes and fails, with grade "C" or better being a pass. here is what I have written:
def Grade(Mark):
    if 0 <= Mark <= 39:
        Grade = "U"
    elif 40 <= Mark <= 49:
        Grade = "E"
    elif 50 <= Mark <= 59:
        Grade = "D"
    elif 60 <= Mark <= 69:
        Grade = "C"
    elif 70 <= Mark <= 79:
        Grade = "B"
    elif 80 <= Mark <= 89:
        Grade = "A"
    elif 90 <= Mark <= 100:
        Grade = "A*"   
    return Grade

def FailPass(Count):
    NumPass = 0
    NumFail = 0
    for Loop in range(Count+1):
        if Grades[Count] not in("C","B","A","A*"):
            NumFail += 1
        else:
            NumPass += 1
    PerPass = (NumPass/Count)*100
    ParFail = (NumFail/Count)*100
    
    return [PerPass,PerFail,NumPass,NumFail]

Grades = []
Counter = 0
Input = 1
while Input != 0:
    Input = int(input("\nWelcome!\n\
Press '1' to convert a mark into a grade\n\
Press '2' to display the amount and percentage of passes\n\
Press '3' to display the amount and percentage of fails\n\
Press '0' to quit\n\
>>> "))
    if Input == 1:
        MarkIn = int(input("\nPlease enter the mark you wish to convert\n\
>>> "))
        if 0 <= MarkIn <= 100:
            GradeOut = Grade(MarkIn)
            print("\nA mark of",MarkIn,"is equivalent to a grade",GradeOut,)
            Grades.append(GradeOut)
            Counter += 1
            
        else:
            print("\nPlease enter a valid mark.")

    elif Input == 2 or Input == 3:
        FP = FailPass(Counter)
        if Input == 2:
            print("\nThere were",FP[3],"passes, which means %"+str(FP[1]),"of all students passed.")
        else:
            print("\nThere were",FP[4],"fails, which means %"+str(FP[2]),"of all students failed.")
but I get the error 'list index out of range' in line 22. I don't know what I am doing wrong. Also any other tips would be appreciated.
Reply
#2
Good try.

It is not usual in python to have a capital at the start of a variable name.

I've only had a quick look through your code, but I can't see where you've initialised your grade list (the results) in order to do any analysis on it, thus you are attempting to loop through an empty list. You need a line to set some test data for now.

Incidentally, with your menu, why bother converting to integer, just test for "0", "1", etc. The while loop can be simpler: while True: and do a break if not input: (as an non-null string is True.

The part of your code outside of functions is a little hard to read. Might be worth moving code into more functions so that it is easier to follow the flow/logic.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,899 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,331 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,917 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,420 May-12-2021, 10:38 AM
Last Post: ranbarr
  List vs index: Frederico_Caldas 5 3,587 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,250 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  list index out of range mcgrim 2 2,906 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,847 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,526 Aug-17-2018, 11:18 PM
Last Post: felie04
  Accessing data in zip - Index out of range pythoneer 24 12,734 Mar-15-2018, 06:19 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020