0

My program is a prefect application quiz and at the begining of the quiz students need to enter their: Name, Lastname and School class. At the end of the quiz the program will calculate the score and if they have passed then their details will be displayed on screen and written to a text file. At this point the user will be asked to write a short statement about why they should be considered to become a prefect and this will also be printed onto the file. The user should be able to print the file off as a receipt that they have passed the prefect application quiz.

This is my code but it is not working, can anyone explain why?

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)

The problem appears on this next line where there is a 'syntax' error:

        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)
6
  • You forgot receipt_printoff.close(), or better use with statement when dealing with file objects. Commented Jan 7, 2014 at 14:22
  • Well what is the syntax error? Commented Jan 7, 2014 at 14:23
  • @dimo414: On the line before where the OP thinks it is.. Commented Jan 7, 2014 at 14:25
  • @MartijnPieters yes I can see it, I wanted OP to tell us what (s)he's seeing. Commented Jan 7, 2014 at 14:26
  • What is a prefect application quiz? Commented Jan 7, 2014 at 14:32

3 Answers 3

4

You have two syntax errors:

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

You are missing a closing parenthesis ) and a : colon.

The first line is what is causing your immediate syntax error, but once you fix that you'll run into the second error.

You probably wanted to write a space and the statement; use string concatenation for that:

receipt_printoff.write(student_name + " " + student_surname)

and

receipt_printoff.write(" " + statement)

or use string formatting; you probably want to add some newlines too:

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

because file.write() is nothing like print(); it won't convert values to string for you, and can only take one argument at a time.

Sign up to request clarification or add additional context in comments.

Comments

0

with open("data.txt", "r") as l should be with open("data.txt", "r") as l: (note the colon).

Comments

0

You're missing a colon from the end of with open("data.txt", "r") as l:.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.