Reputation: 89
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)
Upvotes: 0
Views: 198
Reputation: 1121864
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.
Upvotes: 4
Reputation: 8013
You're missing a colon from the end of with open("data.txt", "r") as l:
.
Upvotes: 0
Reputation: 77902
with open("data.txt", "r") as l
should be with open("data.txt", "r") as l:
(note the colon).
Upvotes: 0