为什么我的程序将<function convert =“”at =“”0x02de8c00 =“”>写入我的文件?</function>

时间:2015-03-10 20:49:35

标签: python

def convert(score): 
    if score >= '90':
        msg = "Congratulations! You earned an A!"
        (msg)

    elif score >= '80':
        msg = "You earned a B; good job!"
        (msg)

    elif score >= '70':
        msg = "You earned a C; looks like you need to study a little harder."
        (msg)

    elif score >= '60':
        msg = "You earned a D; maybe you should consider tutoring"
        (msg)

    else:
        score < '60'
        msg = "I'm sorry; you failed."
        (msg)


    return convert

def main():
    infilename = input("Please enter the name of the input file: ") 
    outfilename = input("Please enter the name of the output file: ")

    infile = open(infilename,"r")
    outfile = open(outfilename,"w")

    for line in infile: 
        msg = convert(line) 
        print(msg,file=outfile)

    infile.close()
    outfile.close()

main()

这些成绩应该打印到文件中,但是当我打开文件时,所有内容都是:<function convert at 0x02DE8C00>。如果我运行代码,并且不尝试将其打印到文件中,它可以正常工作。

好的,他们希望我添加更多详细信息,但我真的无法想到要添加的任何内容,我想你会得到它。

这次我做错了什么?

1 个答案:

答案 0 :(得分:7)

您的函数返回自己(return convert)而不是msg