文件“<string>”,行语法错误:解析时意外的EOF

时间:2017-01-10 08:34:23

标签: python-2.7

我正在尝试做这个练习但是失败了。我只能得到这个结果:

main()   File "C:\Users\chemuser\Desktop\KHL\python\chapter4_6.py", line 22, in main
asciimark = eval(line)   File "<string>", line 1

^ SyntaxError: unexpected EOF while parsing

这是我的失败代码:

# -*- coding: cp1252 -*-
#A certain CS professor gives 5-point quizzes that are 
#graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F.
#Write a program that accepts a quiz score as an 
#input and prints out the corresponding grade.

import string

def main():

    #set up file names
    infileName = raw_input("input file name: ")
    outfileName = raw_input("output file name: ")

    #open files
    infile = open(infileName, "r")
    outfile = open(outfileName, "w")

    #process each line of the input file
    for line in infile.readlines():
        #get the mark from line
        asciimark = eval(line)
        #creat grade
        allgrade = "FEDCBA"
        grade = allgrade[asciimark]
        #write it to the output file
        outfile.write(grade+"\n")

    #close both files
    infile.close()
    outfile.close()

    print "Done!"

main()

输入文件看起来像这样(只是一个数字0 - 5的列,之间没有空行):

5

1

0

2

3

我做了一个测试,我在第一行(5 / n)添加'/ n',它显示:

main()   File "C:\Users\chemuser\Desktop\KHL\python\chapter4_6.py", line 22, in main
asciimark = eval(line)   File "<string>", line 1
5\n
  ^ SyntaxError: unexpected character after line continuation character

您的意见非常感谢!!

0 个答案:

没有答案