I have the following program that reads some data from a csv file but it seems to be resisting all attempts to either read the "marks" data as an integer or convert it to one later on. I based this program on another I have which is fully functional with the only real difference that the integer field was a float in my functioning program.
I've tried entering the following line into the findHighest function just after the "for counter..." line.
**Students[0].marks = int(Students[0].marks)**
The code runs but doesn't find the highest number in the data (it comes back with 96 when the highest number is 100, the second highest is 96). I've also tried altering the following line...
Students[counter].marks = row[3]
and changed it to...
**Students[counter].marks = int(row[3])**
This gives me the following error:
ValueError: invalid literal for int() with base 10: ''
What am I missing here? :-/
import csv
class Student:
def __init__(self,forename,surname,form,marks):
self.forename = ""
self.surname = ""
self.form = ""
self.marks = 0
def readFile():
Students = []
filename = "pupils.csv"
csv_file = open(filename, "r")
reader = csv.reader(csv_file)
counter = 0
for row in reader:
Students.append(Student("", "", "", 0))
Students[counter].forename = row[0]
Students[counter].surname = row[1]
Students[counter].form = row[2]
Students[counter].marks = row[3]
counter = counter + 1
return Students
def findHighest(Students):
position=0
highest = Students[0].marks
for counter in range(len(Students)):
Students[counter].marks = int(Students[counter].marks)
if Students[counter].marks > highest:
highest = Students[counter].marks
position = counter
return highest
def displayHighest(highest):
print("the highest pupil score was:", highest)
Students = readFile()
highest = findHighest(Students)
displayHighest(highest)
CSV File: