Reputation: 41
what i'm trying to do is to read each line of the file then put it into other function called handleLine which then creates output, numericgrade calculates the overall percentage and then lettergrade assigns a grade.
for example in file i have "name, last name", classid, exam, final and i want it to output full name, score , grade
def main():
fin=open("scores.csv", "r")
fout=open("grades.csv", "w")
fout.write('"Name", "Score", "Grade"\n')
line=fin.readlines()[1:]
for line in line:
line=line.split(",")
handleLine(line)
fout.write('"' + lname + "," + fname + '" ,' + score +", " + grade+ ' ", ')
def numericGrade(midterm, final):
score=0.4*float(midterm) + (0.6 * float(final))
def letterGrade(score):
grade = None
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >=70:
grade ="C"
elif score >= 60:
grade = "D"
else:
grade = "F"
def handleLine(line):
fout=open("grades.csv", "w")
fname= line[1][1 : -1]
lname= line[0][1 : ]
midterm= line[2]
final = line[3]
numericGrade(midterm, final)
score = numericGrade(midterm,final)
grade = letterGrade(score)
return lname, fname, score, grade
if __name__=="__main__":
main()
I'm having trouble putting one function to another, now it says that lname is not defined in line 14. Im really lost now.
EDIT: I have fixed the
lname, fname, score, grade= handleLine(line)
but i have an error now in line 14
TypeError: cannot concatenate 'str' and 'NoneType' objects
Upvotes: 0
Views: 76
Reputation: 10351
When you return a value from a function, the name of the value is not what it was inside the function, but what you call it when the function returns.
def myfunction():
num = 5
return num #returns 5
variableThatIsNotCalledNum = myfunction()
# my long named variable now holds the value 5
#error: no such variable around here
print num
# prints the 5
print variableThatIsNotCalledNum
One other thing, numericGrade()
and letterGrade()
need to return
score
and grade
respectively.
Upvotes: 3
Reputation: 799210
Only values are returned, not names.
lname, fname, score, grade = handleLine(line)
Upvotes: 3