编写面向批处理的程序

时间:2015-04-19 04:02:14

标签: python file python-3.x

教授进行100分考试,分数为90-100:A,80-89:B等。输入文件为exam.txt,必须写入grade.txt。这是我的代码,但效果不好:

infile = open("exam.txt","r")
outfile = open("grade.txt","w")

for line in infile:
    line = int(line)
    if line>=90:
        print("A", file=outfile)
    elif line>=80 and line<=89:
        print("B", file=outfile)
    elif line>=70 and line<=79:
        print("C", file = outfile)
    elif line>=60 and line<=69:
        print("D", file=outfile)
    elif line>=50 and line<=59:
        print("E", file=outfile)
    else:
        print("F", file= outfile)

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

infile = open("fin.txt","r")
outfile = open("grade.txt","w")

for line in infile:
    line = int(line)
    if line>=90:
        outfile.write("%s --> A \n" %line)
    elif line>=80 and line<=89:
        outfile.write("%s --> B \n" %line)
    elif line>=70 and line<=79:
        outfile.write("%s --> C \n" %line)
    elif line>=60 and line<=69:
        outfile.write("%s --> D \n" %line)
    elif line>=50 and line<=59:
        outfile.write("%s --> E \n" %line)
    else:
        outfile.write("%s --> F \n" %line)

outfile.close()