from pyscript import document, when
# This class represents a single classmate
# It stores the name, section, and favorite subject
class Classmate:
def __init__(self, name, section, favorite_subject):
self.name = name
self.section = section
self.favorite_subject = favorite_subject
# This method returns a formatted sentence introducing the classmate
def introduce(self):
return f"Hi! I'm {self.name} from {self.section}. My favorite subject is {self.favorite_subject}."
# This list is used to store all Classmate objects.
# Instead of starting empty, it is pre-filled with sample data
# Demonstrates object creation as required by the task.
classmates = [
Classmate("Joshua", "10-Emerald", "English"),
Classmate("Wilwen", "10-Ruby", "Math"),
Classmate("Opdesh", "10-Sapphire", "ICT"),
Classmate("Matt Sky", "10-Amethyst", "Science"),
Classmate("Lewis", "10-Luna", "Social Studies")
]
# This function runs when the "Add to List" button is clicked
@when("click", "#addBtn")
def add_classmate(event):
# Retrieve values entered by the user
name = document.getElementById("name").value
section = document.getElementById("section").value
subject = document.getElementById("subject").value
# Only proceed if all fields contain values
if name and section and subject:
# Create a new Classmate object and store it in the list
classmates.append(Classmate(name, section, subject))
# Clear the input fields after adding the data
document.getElementById("name").value = ""
document.getElementById("section").value = ""
document.getElementById("subject").value = ""
# This function runs when the "Show List" button is clicked
@when("click", "#showBtn")
def show_list(event):
# Access the output div where the list will be displayed
output_div = document.getElementById("output")
# Clear previous content to prevent duplication
output_div.innerHTML = ""
# Loop through each stored classmate
for c in classmates:
# Display each classmate's introduction in paragraph form
output_div.innerHTML += f"