尝试在python中写入文本文件时出现语法错误

时间:2014-01-07 14:21:12

标签: python file text input

我的课程是一个完美的应用测验,在测验开始时,学生需要输入:NameLastnameSchool class。在测验结束时,程序将计算得分,如果已经通过,那么他们的详细信息将显示在屏幕上并写入文本文件。此时,将要求用户写一个简短的声明,说明为什么应该将它们视为省长,这也将打印在文件上。用户应该能够将文件打印为已通过完美应用程序测验的收据。

这是我的代码,但它没有用,有人可以解释原因吗?

class Score_Window(tk.Toplevel):
    '''A simple instruction window'''
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        score_str = str(sum(parent.score_per_question.values()))
        self.score = tk.Label(self, width=80, height=4, text = "You're score was: " + score_str)
        self.score.pack(side="top", fill="both", expand=True)


        if int(score_str) >= 3:
            print("Pass")

            self.prefect = tk.Label(self, width=80, height=4, text = "You have passed, well done! You can now become a prefect.")
            self.prefect.pack(side="top", fill="both", expand=True)

            self.name = tk.Label(self, width=80, height=4, text = student_name)
            self.name.pack(side="top", fill="both", expand=True)

            self.surname = tk.Label(self, width=80, height=4, text = student_surname)
            self.surname.pack(side="top", fill="both", expand=True)

            self.tutor = tk.Label(self, width=80, height=4, text = student_tutor_group)
            self.tutor.pack(side="top", fill="both", expand=True)

            receipt_printoff = open("data.txt", "w")
            receipt_printoff.write(student_name, " ",student_surname)
            receipt_printoff.write(student_tutor_group)
            statement = tkSimpleDialog.askstring("User data", "Enter something about yourself")
            receipt_printoff.write((" ",statement)

问题出现在下一行,其中存在“语法”错误:

        with open("data.txt", "r") as l
            for lines in l:
                student_name2, student_surname2, statement2 = lines.split()
                namelabel = label(self, text=student_name2)
                namelabel.pack()
                surnamelable = label(self, text=student_surname2)
                surnamelable.pack()
                Userinfolabel = label(self, text=statement2)
                Userinfolabel.pack()
    else:
        print("Fail")

        self.fail = tk.Label(self, width=80, height=4, text = "Unfortunately you have not scored highly enough to be considered for a prefect position.")
        self.fail.pack(side="top", fill="both", expand=True)

3 个答案:

答案 0 :(得分:4)

您有两个语法错误:

        receipt_printoff.write((" ",statement)
        #  -----------------------------------^
    with open("data.txt", "r") as l
    # -----------------------------^

您缺少右括号) :冒号。

第一行是导致您立即出现语法错误的原因,但一旦修好,您将遇到第二个错误。

你可能想写一个空格和陈述;使用字符串连接:

receipt_printoff.write(student_name + " " + student_surname)

receipt_printoff.write(" " + statement)

或使用字符串格式;你可能也希望添加一些换行符:

receipt_printoff.write('{} {}\n'.format(student_name, student_surname))
# ...
receipt_printoff.write(' {}\n'.format(statement))

因为file.write()print()完全不同;它不会为您将值转换为字符串,并且一次只能使用一个参数。

答案 1 :(得分:0)

with open("data.txt", "r") as l应为with open("data.txt", "r") as l:(请注意冒号)。

答案 2 :(得分:0)

你在with open("data.txt", "r") as l:的末尾错过了冒号。