Python Forum
'Exception Has occured: UnBoundLocalError'
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

[Image: 638c398bd758a99597b5b38aae5ecee2]
[Image: 4d7d6862387ca59269cb2a35dd427793]

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()
 
Reply
#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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does UnboundLocalError work? deanhystad 3 1,633 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,857 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  exec + subprocess = UnboundLocalError paul18fr 6 3,443 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,229 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 2,621 May-31-2020, 07:22 AM
Last Post: azajali43
  why this error occured in recursion ashishraikwar 1 1,737 Apr-24-2020, 11:12 AM
Last Post: buran
  UnboundLocalError: local variable referenced before assignment svr 1 3,261 Dec-27-2019, 09:08 AM
Last Post: perfringo
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 5,433 Sep-03-2019, 07:22 PM
Last Post: buran
  Sorry to bother, I've occured a newbiw question. Ethan4216 2 1,686 Aug-30-2019, 05:49 AM
Last Post: Ethan4216
  UnboundLocalError Problem DrChicken24 1 2,186 Mar-27-2019, 02:53 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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