Molécules

This commit is contained in:
un-oeil 2026-11-15 14:02:04 +01:00
parent f7212eb184
commit 32cd698d35

36
main.py
View file

@ -112,3 +112,39 @@ class Oxygen(Atom):
print(Hydrogen()) print(Hydrogen())
print(Carbon()) print(Carbon())
print(Oxygen()) print(Oxygen())
# Molécule
class Molecule(ChemicalComponent):
def __init__(self, name, atoms_list):
super().__init__(name)
for atom in atoms_list:
if not isinstance(atom, Atom):
raise TypeError("La list d'atomes ne contient pas que des atomes")
self.__atoms_list = atoms_list # Doit être du type list
def mass(self):
m = 0
for atom in self.__atoms_list:
m += atom.mass()
return m
def affichage(self):
d= dict()
for atom in self.__atoms_list:
if type(atom) in d:
d[type(atom)]+=1
else:
d[type(atom)]=1
return d
def __str__(self):
s = ""
for t, n in self.affichage().items():
if n > 1:
s += f"{t.symbol()}{n}"
else:
s += t.symbol()
return s