0% found this document useful (0 votes)
13 views2 pages

Corrige TEST PYTHON 24 - 25

Uploaded by

bedairiainel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Corrige TEST PYTHON 24 - 25

Uploaded by

bedairiainel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Define two classes: Student and Teacher.

 Student Class:
Attributes:
student_id (string), name (string), grades (list of floats)
Methods:
 add_grade(self, grade): Add a grade to the list.
 calculate_average(self): Return the average of all grades.
 Teacher Class:
Attributes: teacher_id (string), name (string), subjects (set of strings)
Methods:
 add_subject(self, subject): Add a subject to the set.
 remove_subject(self, subject): Remove a subject if it exists.

2. Create 3 Student objects and 2 Teacher objects.


3. Store all Student objects in a list and all Teacher objects in a second list.
4. Loop through the list to add 3 grades (user input) for each student and calculate their
averages.
5. Add 3 subjects to each teacher's subjects set.
6. Remove one subject for a teacher and display the final set.
7. Identify students with an average grade above 10.
8. Display their names.

-----------------------------------------------------------------------

class Student:
def __init__(self, student_id, name):
self.student_id = student_id
self.name = name
self.grades = []

def add_grade(self, grade):


self.grades.append(grade)

def calculate_average(self):
return sum(self.grades) / len(self.grades) if self.grades else 0

class Teacher:
def __init__(self, teacher_id, name):
self.teacher_id = teacher_id
self.name = name
self.subjects = set() # ou bien self.subjects = {}

def add_subject(self, subject):


self.subjects.add(subject)

def remove_subject(self, subject):


self.subjects.discard(subject)
students = [ Student("S01", "Amine"), Student("S02", "Ibrahim"), Student("S03", "Fatima")]
teachers = [Teacher("T01", "Mrs. Arezki"), Teacher("T02", "Mr. Khelif")]

for student in students:


print(f"Enter grades for {student.name}:")
for i in range(3): # Allow 3 grades per student
grade = float(input("Enter grade: "))
student.add_grade(grade)

print("Student Averages:")
for student in students:
avg = student.calculate_average()
print(f"{student.name}: {avg:.2f}")

teachers[0].add_subject("Math")
teachers[0].add_subject("Python")
teachers[0].add_subject("CPP")
teachers[0].remove_subject("CPP")

teachers[1].add_subject("English")
teachers[1].add_subject("Sensors")

print("\nTeacher Subjects:")
for teacher in teachers:
print(f"{teacher.name}: {', '.join(teacher.subjects)}")

print("\nStudents with an average above 10:")


for student in students:
if student.calculate_average() > 10:
print(student.name)

You might also like