-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQuiz.py
More file actions
102 lines (83 loc) · 5.87 KB
/
PythonQuiz.py
File metadata and controls
102 lines (83 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tkinter as tk # Import tkinter library for GUI and give it ***alias tk
from tkinter import messagebox # Import messagebox for popup
# This class holds/contains a single Exam question
class Question:
def __init__(self, prompt, options, answer):
self.prompt = prompt # The text of the question
self.options = options # list of possible answer options
self.answer = answer # The correct answer string
# This class manages the exam: questions, score, and current question
class Exam:
def __init__(self, questions):
self.original_questions = questions[:] # Make a copy of the original questions list
self.reset_exam() # *Initialize* the exam state
def reset_exam(self):
self.questions = self.original_questions[:] # Reset questions list from original
self.question_index = 0 # Start at the first question
self.score = 0 # Start with a score of 0
def get_current_question(self):
return self.questions[self.question_index] # Return the current question obj
def check_answer(self, selected):
current = self.get_current_question() # Get the current questiom
if current.answer == selected: # Check if selected answer is correct
self.score += 1 # Add a point if the answer is correct
self.question_index += 1 # Move to the next question
return self.question_index < len(self.questions) # Return True if more questions are left
# GUI class to handle user interface with/using tkinter GUI
class ExamApp:
def __init__(self, root, exam):
self.root = root # The main tkinter window
self.exam = exam # The Exam object containing Exam
self.selected_option = tk.StringVar() # A variable to hold the selected answer
self.root.title("Final Exam") # Set the window title
# Create the question label
self.question_label = tk.Label(root, text="", wraplength=400, font=("Arial", 14)) # Label for question text (appearance)
self.question_label.pack(pady=20) # Add label to window with vertical padding (for appearance and usability)
self.options = [] # List to store radio buttons for answer options ***Radio buttons allow you to only select one option***
for i in range(4): # Loop to create 4 radio buttons (4 questions)
rb = tk.Radiobutton(root, text="", variable=self.selected_option, value="", font=("Arial", 12)) # Create a radio button
rb.pack(anchor="w", padx=20, pady=5) # Add button to the window, left-aligned
self.options.append(rb) # Add the radio button to the list
# Create the Submit button
self.submit_button = tk.Button(root, text="Submit", command=self.submit_answer) # a Button to submit a selected answer
self.submit_button.pack(pady=10) # Add button with padding (visual)
# Create the Restart button
self.restart_button = tk.Button(root, text="Restart Exam", command=self.restart_exam) # Button to restart exam if you need to rescore :)
self.restart_button.pack(pady=10) # Add button with padding
self.restart_button.config(state=tk.DISABLED) # Disable restart button at the start
self.load_question() # Load and display the first question
def load_question(self):
q = self.exam.get_current_question() # Get the current question
self.selected_option.set(None) # Clear any selected radio button
self.question_label.config(text=q.prompt) # Set question label text
for i, option in enumerate(q.options): # Loop through the 4 options
self.options[i].config(text=option, value=option) # Set the text and value of each radio button
def submit_answer(self):
selected = self.selected_option.get() # Get the selected answer
if not selected: # If no option was selected
messagebox.showwarning("No answer", "Please select an option.") # Show warning popup
return # Exit the function early
if self.exam.check_answer(selected): # Check if the answer is correct and move to next question
self.load_question() # Load the next question
else:
# If no more questions left
messagebox.showinfo("Exam Completed", f"You scored {self.exam.score}/{len(self.exam.questions)}") # Show final score popup
self.submit_button.config(state=tk.DISABLED) # Disable Submit button
self.restart_button.config(state=tk.NORMAL) # Enable Restart button
def restart_exam(self):
self.exam.reset_exam() # Reset exam data (score and questions)
self.submit_button.config(state=tk.NORMAL) # Enable Submit button
self.restart_button.config(state=tk.DISABLED) # Disable Restart button again
self.load_question() # Load the first question again
# --- The actual questions ---
question_list = [
Question("Jimmy's father has three sons- Paul I and Paul II. Can you guess the name of the third son? ", ["Paul III", "Jerin", "Jimmy", "None"], "Jimmy"), # Create question 1
Question("You're 4th place right now in a race. What place will you be in when you pass the person in 3rd place? ", ["1st", "2nd", "3rd", "Saturn"], "None of the ABove"), # Question 2
Question("How many months have 28 days? ", ["2", "1", "Depends on if the year is Leap", "All of Them"], "All of them"), # Question 3
Question("If you have a bowl with six apples and you take away four, how many apples do you have? ", ["2 Apples", "6 Apples", "4 Apples", "None"], "4 Apples"), # Question 4
]
# Start the app
exam = Exam(question_list) # Create Exam object with the question list
root = tk.Tk() # Create the main tkinter window
app = ExamApp(root, exam) # Create the ExamApp (GUI) with the window and exam logic
root.mainloop() # Start the GUI event loop (keeps window open)