解析时意外的EOF

时间:2016-03-10 10:45:18

标签: python python-3.x

我正在开发一个程序,该程序可以从各种文本文件中调用数据,并以各种用户选择的方式显示它。我对Python不是很熟悉,我的代码中可能存在很多错误。当我运行代码时出现unexepected EOF while parsing错误。有人知道这里有什么问题吗?

from time import sleep
import sys

for i in range(21):
    sys.stdout.write('\r')
    sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
    sys.stdout.flush()
    sleep(0.25)

print('\nWelcome to The Arithmatic Quiz: Teacher Eddition.')

UserName = input('Please input your user name and press enter.')

if UserName == 'MrSmith':
    print ('Please proceed.')

else:
    quit()

Password = input('Please input your password and press enter.')

if Password == 'Maths':
    print ('Welcome Mr Smith.')

else:
    quit()

import csv
import operator

classdata = 0
while classdata == 0:

    Class = input ('Which class do you want the data for?')

    Display = input ('How do you want the data t be sorted? Alphabetically      (A), on highest score for the first try (H1), on highest score for the second try (H2), on highest score for the third try (H3), or average score (Avg)?')

if Class == '1':
    Class_1 = open('class_1.txt','r')

    csv1 = csv.reader(Class_1,delimiter=',')

    if Display == 'A':
        sortA = sorted(csv1,key=operator.itemgetter(1))

        for eachline in sortA:
            print (eachline)

    if Display == 'H1':
        sortB = sorted(csv1,key=operator.itemgetter(2))

        for eachline in sortB:
            print (eachline)

    if Display == 'H12':
        sortB = sorted(csv1,key=operator.itemgetter(3))

        for eachline in sortB:
            print (eachline)

    if Display == 'H3':
        sortB = sorted(csv1,key=operator.itemgetter(4))

        for eachline in sortB:
            print (eachline)

    elif Display == 'Avg':
        sortC = sorted(csv1,key=operator.itemgetter(5))

        import csv
        from collections import Counter

        def average_column (csv_filepath):
            row_totals = Counter()
            with open('Class_1',"rb") as f:
                reader = csv.reader(f)
                column_count = 0.0
                for column in reader:
                    for row_idx, row_value in enumerate(column):
                        try:
                            n = float(row_value)
                            row_totals(row_idx) += n
                            column_count += 1.0
                            column_count -= 1.0
                            row_indexes = row_totals.keys()
                            row_indexes.sort()
                            averages = (row_totals(idx)/column_count for idx in row_indexes)
                            print(averages)

如果有帮助,我正在运行Python 3.4.3

1 个答案:

答案 0 :(得分:0)

您错过了代码末尾except / try循环的except部分。如果您有try声明,那么您也必须拥有except声明,如果您没有声明,则会显示您看到的错误。以下是缺少except语句的代码部分:

try:
    n = float(row_value)
    row_totals(row_idx) += n
    column_count += 1.0
    column_count -= 1.0
    row_indexes = row_totals.keys()
    row_indexes.sort()
    averages = (row_totals(idx)/column_count for idx in row_indexes)
    print(averages)
except:   # here is the except statement you were missing
    pass