<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_results(self,folder=None, filename='results.txt', to_file=True):
 """ Writes text file of calculation summary. :param folder: (optional) Folder to which to write ``results.txt``. :param filename: Filename to write. Default=``results.txt``. :param to_file: If True, then writes file. Otherwise just return header, line. :returns: Header string, line """

        if folder is None:
            folder = self.folder
        if to_file:
            fout = open(os.path.join(folder,filename), 'w')
        header = ''
        for m in self.popset.shortmodelnames:
            header += 'lhood_{0} L_{0} pr_{0} '.format(m)
        header += 'fpV fp FPP'
        if to_file:
            fout.write('{} \n'.format(header))
        Ls = {}
        Ltot = 0
        lhoods = {}
        for model in self.popset.modelnames:
            lhoods[model] = self.lhood(model)
            Ls[model] = self.prior(model)*lhoods[model]
            Ltot += Ls[model]

        line = ''
        for model in self.popset.modelnames:
            line += '%.2e %.2e %.2e ' % (lhoods[model], Ls[model], Ls[model]/Ltot)
        line += '%.3g %.3f %.2e' % (self.fpV(),self.priorfactors['fp_specific'],self.FPP())

        if to_file:
            fout.write(line+'\n')
            fout.close()

        return header, line