简体   繁体   English

将一个功能放到另一个功能上遇到麻烦

[英]trouble putting one function to another

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. 我想做的是读取文件的每一行,然后将其放入另一个称为handleLine的函数中,该函数然后创建输出,numericalgrade计算总百分比,然后lettergrade分配等级。

for example in file i have "name, last name", classid, exam, final and i want it to output full name, score , grade 例如,在文件中,我有“姓名,姓氏”,classid,考试,最终成绩,我希望它输出全名,成绩,成绩

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. 我在将一个函数放到另一个函数时遇到了麻烦,现在它说在第14行中未定义lname。

EDIT: I have fixed the 编辑:我已经修复了

lname, fname, score, grade= handleLine(line) lname,fname,score,grade = handleLine(line)

but i have an error now in line 14 但是我现在在第14行有一个错误

TypeError: cannot concatenate 'str' and 'NoneType' objects TypeError:无法连接“ str”和“ NoneType”对象

仅返回 ,而不返回名称。

lname, fname, score, grade = handleLine(line)

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. 另一件事, numericGrade()letterGrade()需要分别return scoregrade

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM