Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 'Exception Has occured: UnBoundLocalError'
#1
Im fairly new to coding, but im having an issue with this program. The main function of the program is the read data from an external .csv file which contains, a students fore and surname, their 'coursemark' and 'exammark'. Then is supposed works out what grade each individual student achieved by calculating the combination of there 'coursemark' and 'exammark'.
The program im having though is that the program is only displaying 7 of the 15 student's grades, then crashes and displays an error. Im really not sure why this is happening so if someone could have a look at the screenshots below and see if they know whats going on and give me some advice it would be very much appreciated. Dance

İmage

İmage


EDIT: Heres the code.
def grade(overallpercentage):
    percentage = overallpercentage/150*100
    if percentage >= 70:
        grade="You have achieved an A grade"   
    elif percentage >= 60 and percentage <= 69:   
        grade="You have achieved a B grade"

    elif percentage >= 50 and percentage <= 59:   
        grade="You have achieved a C grade"

    elif percentage < 45:  
        grade="No Grade"

    return grade,percentage

def main():

    f = open("Higher  Supplementary Files Package 3.csv")
    
    rows = f.readlines()

    for counter in range(len(rows)):
      
        line = rows[counter].strip() 
        
        fields = line.split(",")

        student_name = fields[0]
        course_mark = fields[1]
        exam_mark = fields[2]

        course_mark = int(course_mark)
        prelim_mark = int(exam_mark)

        overallpercentage= course_mark + prelim_mark
        newgrade, newpercentage = grade(overallpercentage)

        print(50 * "~")
        print("Name:",student_name)
        print("OverallPercentage: ",newpercentage)
        print("Grade: ",newgrade) 

    print()

if __name__ == '__main__':
    main()
 
Quote
#2
For starters just style suggestion: use comparisons in more readable format. You can do this way:

>>> percentage = 65                                                        
>>> if 60 <= percentage <= 69: 
...     print('In range')
...
In range
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 113 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  problem using custom exception handling in python srm 3 175 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 415 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception name for a flagged stop Skaperen 1 521 May-31-2019, 04:33 AM
Last Post: heiner55
  name of exception for exit() Skaperen 2 235 May-24-2019, 07:07 PM
Last Post: Skaperen
  exception handling KyawMyo 3 266 May-07-2019, 07:53 AM
Last Post: buran
  Stack trace shows different exception type than print micseydel 5 539 Apr-01-2019, 10:24 PM
Last Post: micseydel
  UnboundLocalError Problem DrChicken24 1 244 Mar-27-2019, 02:53 AM
Last Post: ichabod801
  Database operation exception handling LostInCode 1 338 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain
  During handling of the above exception, another exception occurred Skaperen 7 3,696 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)