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


Messages In This Thread
list index out of range - by DrPengin - Nov-09-2017, 08:00 PM
RE: list index out of range - by gruntfutuk - Nov-09-2017, 08:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 6,228 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,398 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,961 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,590 May-12-2021, 10:38 AM
Last Post: ranbarr
  List vs index: Frederico_Caldas 5 3,669 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,312 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  list index out of range mcgrim 2 2,951 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,948 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,581 Aug-17-2018, 11:18 PM
Last Post: felie04
  Accessing data in zip - Index out of range pythoneer 24 12,974 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