Skip to content

Instantly share code, notes, and snippets.

@akirap3
Created December 18, 2020 12:15
Show Gist options
  • Save akirap3/f9308543666b01e869f51a74e5c394aa to your computer and use it in GitHub Desktop.
Save akirap3/f9308543666b01e869f51a74e5c394aa to your computer and use it in GitHub Desktop.
class StudentsDataException(BaseException):
    pass 

class BadLine(StudentsDataException):
    def __str__ (self):
        return "There is bad lines or wrong scores." 

class FileEmpty(StudentsDataException):
    def __str__ (self):
        return "There is no data in the file." 

def closeFilesAndExit(src, dst, e):
    src.close()
    dst.close()
    return e.__str__()

try:
    srcname = input("Enter the source file: ")
    dstname = input("Enter the destination file: ")
    src = open(srcname, 'rt')  
    dst = open(dstname, 'wt')
    
    data = src.readlines()
    if data == []:
        raise FileEmpty()
    try:
        studentDict = {}
        for i in data:
            newData = i.strip().replace('\t',' ')
            studentData = newData.split(' ')
            student = studentData[0] + '\t'+studentData[1] +'\t'
            studentOneScore = studentData[2]                 
            if student in studentDict.keys():
                temp = {student: float(studentDict[student])+float(studentOneScore)}
            else:
                temp = {student: float(studentOneScore)}
            studentDict.update(temp)

        for k in sorted(studentDict.keys()):
            temp = k + str(studentDict[k]) + '\n'
            dst.write(temp)
    except:
        raise BadLine()

    src.close()
    dst.close()
                
except FileEmpty as fe:
    message = closeFilesAndExit(src, dst, fe)
    print(message)
except BadLine as bl:
    message = closeFilesAndExit(src, dst, bl)
    print(message)
except BaseException as e:
    message = closeFilesAndExit(src, dst, e)
    print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment