From 64b8207dbba00eaf266ffed3a1babe65d56f5bcd Mon Sep 17 00:00:00 2001 From: yann Date: Mon, 17 Feb 2025 16:52:06 +0100 Subject: [PATCH] publish solutions --- Exercice01/main.py | 3 +++ Exercice02/main.py | 16 ++++++++++++ Exercice03/main.py | 6 +++++ Exercice04/main.py | 23 +++++++++-------- Exercice05/main.py | 2 ++ Exercice06/main.py | 8 +++++- Exercice07/main.py | 14 ++++++++++- Exercice08/main.py | 12 +++++++-- Exercice09/main.py | 12 +++++++++ Exercice10/main.py | 25 +++++++++++++++++++ Exercice11/main.py | 31 +++++++++++++++++++++++ Exercice12/main.py | 62 ++++++++++++++++++++++++++++++++++++++++++++-- 12 files changed, 197 insertions(+), 17 deletions(-) diff --git a/Exercice01/main.py b/Exercice01/main.py index dbdf263..5d8b432 100644 --- a/Exercice01/main.py +++ b/Exercice01/main.py @@ -1,2 +1,5 @@ name = input("Entrez votre nom : ") age = int(input("Entrez votre âge : ")) + +print(f"Bonjour, je m'appelle {name} et j'ai {age} ans.") + diff --git a/Exercice02/main.py b/Exercice02/main.py index eb53c6a..806007f 100644 --- a/Exercice02/main.py +++ b/Exercice02/main.py @@ -15,3 +15,19 @@ students = { 'Histoire': 78 } } + +name = input("Entrez le nom de l'étudiant : ") +if name in students: + total = 0 + print(f"Notes de {name}:") + for i in students[name]: + note = students[name][i] + total += note + print(i, ":", note) + print(f"Moyenne de {name} : {total/len(students[name])}") + +else: + print("L'étudiant", name, "n'existe pas.") + + + diff --git a/Exercice03/main.py b/Exercice03/main.py index 4ce9b26..c2f3c64 100644 --- a/Exercice03/main.py +++ b/Exercice03/main.py @@ -1 +1,7 @@ words = ["python", "programmation", "langage", "ordinateur", "apprentissage"] +comprehension = [] + +for i in words: + comprehension.append((i, len(i))) + +print(comprehension) diff --git a/Exercice04/main.py b/Exercice04/main.py index 22e343c..71938fa 100644 --- a/Exercice04/main.py +++ b/Exercice04/main.py @@ -1,14 +1,15 @@ class myClass: -def __init__(self, full_name): -self.full_name=full_name - -def displayName(self): -print("Le nom complet est :",self.full_name) + def __init__(self, full_name): + self.full_name = full_name + + def displayName(self): + print("Le nom complet est :", self.full_name) + class other_class: -def __init__(self, first_name, name): -self.first_name=first_name -self.name=name - -def display_name(self): -print(f"Nom complet : {self.first_name} {self.name}") + def __init__(self, first_name, name): + self.first_name = first_name + self.name = name + + def display_name(self): + print(f"Nom complet : {self.first_name} {self.name}") diff --git a/Exercice05/main.py b/Exercice05/main.py index a5afa83..78509f8 100644 --- a/Exercice05/main.py +++ b/Exercice05/main.py @@ -1,5 +1,7 @@ def sum(a, b): + """Returns the sum of the two given numbers""" return a + b def subtraction(a, b): + """Returns the difference between first and second given numbers""" return a - b diff --git a/Exercice06/main.py b/Exercice06/main.py index 314311d..105d21b 100644 --- a/Exercice06/main.py +++ b/Exercice06/main.py @@ -1,5 +1,11 @@ # Fonction calculate_average - +def calculate_average(list): + """Returns average of a given list""" + total = 0 + for i in list: + total += i + return total / len(list) + # Exemple d'utilisation de la fonction numbers = [10, 20, 30, 40, 50] average = calculate_average(numbers) diff --git a/Exercice07/main.py b/Exercice07/main.py index 0ddc36d..81a233f 100644 --- a/Exercice07/main.py +++ b/Exercice07/main.py @@ -1 +1,13 @@ -## Écrivez votre code ici ! +def square(): + """returns the square of the number""" + try: + a = input("Entrez un nombre : ") + result = int(a) * int(a) + return result + + except ValueError: + print("Le paramètres doit être un nombre !") + return None + + +print(square()) diff --git a/Exercice08/main.py b/Exercice08/main.py index 854bdd6..58201f6 100644 --- a/Exercice08/main.py +++ b/Exercice08/main.py @@ -1,6 +1,14 @@ def log_decorator(func): - pass - + + def wrapper(): + print("message avant") + result = func() + print("message après") + return result + + return wrapper + + @log_decorator def function_test(): print("Cette fonction ne prend pas d'arguments.") diff --git a/Exercice09/main.py b/Exercice09/main.py index dcf022b..57a7583 100644 --- a/Exercice09/main.py +++ b/Exercice09/main.py @@ -1,3 +1,15 @@ +class Rectangle: + def __init__(self, width, length): + self.width = width + self.length = length + + def calculate_area(self): + return self.width * self.length + + def calculate_perimeter(self): + return 2 * (self.width + self.length) + + # Test de la classe Rectangle rectangle = Rectangle(5, 3) # 5:width & 3:length print("Largeur:", rectangle.width) diff --git a/Exercice10/main.py b/Exercice10/main.py index 0ddc36d..b77087c 100644 --- a/Exercice10/main.py +++ b/Exercice10/main.py @@ -1 +1,26 @@ ## Écrivez votre code ici ! +class Person: + def __init__(self, name, age): + self.name = name + self.age = age + + def display_details(self): + print(self.name, self.age) + + +class Employee(Person): + def __init__(self, name, age, salary): + self.salary = salary + super().__init__(name, age) + + def display_details(self): + super().display_details() + print(self.salary) + + +test = Employee("test", 2, 2200) + +test.display_details() + + + diff --git a/Exercice11/main.py b/Exercice11/main.py index 0ddc36d..569e8fe 100644 --- a/Exercice11/main.py +++ b/Exercice11/main.py @@ -1 +1,32 @@ ## Écrivez votre code ici ! +class BankAccount: + """A bank account""" + def __init__(self, account_holder, balance): + self.account_holder = account_holder + self.balance = balance + + def deposit(self, amount): + """Add money on bank account""" + if amount < 0: + print("Le crédit doit être positif") + else: + self.balance += amount + return self.balance + + def withdraw(self, amount): + """take money from bank account""" + total = self.balance - amount + if amount < 0: + print("Le montant doit être positif") + else: + if total < 0: + print("Le montant disponible est insuffisant") + else: + self.balance -= amount + return self.balance + + def display_balance(self): + """displays informations""" + print("Propriétaire du compte :", self.account_holder) + print("Solde :", self.balance) + diff --git a/Exercice12/main.py b/Exercice12/main.py index 37c627d..eeb3fce 100644 --- a/Exercice12/main.py +++ b/Exercice12/main.py @@ -4,8 +4,66 @@ class Book: self.author = author self.year = year + def __str__(self): + return self.title + + def __repr__(self): + return str() + class Library: def __init__(self): self.books = [] - self.borrow_books = [] - # Ajouter les méthodes ici + self.borrow_books = [] + +# Ajouter les méthodes ici + def add_book(self, book): + """add book to library""" + self.books.append(book) + + def remove_book(self, book_title): + """remove book from library""" + for i in self.books: + if i.title == book_title: + self.books.remove(i) + + def borrow_book(self, book_title): + """borrow a book from library""" + for i in self.books: + if i.title == book_title: + self.books.remove(i) + self.borrow_books.append(i) + + def return_book(self, book_title): + for i in self.borrow_books: + if i.title == book_title: + self.borrow_books.remove(i) + self.books.append(i) + + def available_books(self): + book_list = [] + for book in self.books: + book_list.append(book.title) + return book_list + + def borrowed_books(self): + book_list = [] + for book in self.borrow_books: + book_list.append(book.title) + return book_list + + + + +bib = Library() +book1 = Book("Croc blanc", "Jack London", 1889) +book2 = Book("CRP", "Kant", 1781) + +bib.add_book(book1) +bib.add_book(book2) + +print(bib.available_books()) + +bib.borrow_book("CRP") + +print(bib.available_books()) +print(bib.borrowed_books())