fin du TP
This commit is contained in:
parent
d1380e35d1
commit
2d2d7c3626
1 changed files with 47 additions and 4 deletions
51
main.py
51
main.py
|
|
@ -148,6 +148,11 @@ class Molecule(ChemicalComponent):
|
||||||
s += t.symbol()
|
s += t.symbol()
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
l = [atom for atom in self.__atoms_list]
|
||||||
|
self.__atoms_list = []
|
||||||
|
return l
|
||||||
|
|
||||||
# Méthane
|
# Méthane
|
||||||
|
|
||||||
class Methane(Molecule):
|
class Methane(Molecule):
|
||||||
|
|
@ -192,8 +197,46 @@ class Water(Molecule):
|
||||||
raise TypeError("Des atomes n'appartenant pas à la molécule se trouvent dans la liste")
|
raise TypeError("Des atomes n'appartenant pas à la molécule se trouvent dans la liste")
|
||||||
super().__init__(name, atoms_list)
|
super().__init__(name, atoms_list)
|
||||||
|
|
||||||
|
# Réaction chimique : CH4 + O2 + O2 → CO2 + H2O + H2O
|
||||||
|
|
||||||
print(Methane(), Methane().mass())
|
def ChemicalReaction(molecules):
|
||||||
print(Dioxygen(), Dioxygen().mass())
|
# Chercher un méthane et deux dioxygènes dans la liste
|
||||||
print(CarbonDioxide(), CarbonDioxide().mass())
|
methane = None
|
||||||
print(Water(), Water().mass())
|
dioxygens = []
|
||||||
|
for molecule in molecules:
|
||||||
|
if isinstance(molecule, Methane) and methane is None:
|
||||||
|
methane = molecule
|
||||||
|
elif isinstance(molecule, Dioxygen) and len(dioxygens) < 2:
|
||||||
|
dioxygens.append(molecule)
|
||||||
|
|
||||||
|
# Pas assez de réactifs : pas de réaction
|
||||||
|
if methane is None or len(dioxygens) < 2:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Retirer les réactifs de la liste
|
||||||
|
molecules.remove(methane)
|
||||||
|
for o2 in dioxygens:
|
||||||
|
molecules.remove(o2)
|
||||||
|
|
||||||
|
# Récupérer les atomes des réactifs
|
||||||
|
atoms = methane.destroy()
|
||||||
|
for o2 in dioxygens:
|
||||||
|
atoms += o2.destroy()
|
||||||
|
|
||||||
|
# Trier les atomes par type
|
||||||
|
carbons = [a for a in atoms if isinstance(a, Carbon)]
|
||||||
|
hydrogens = [a for a in atoms if isinstance(a, Hydrogen)]
|
||||||
|
oxygens = [a for a in atoms if isinstance(a, Oxygen)]
|
||||||
|
|
||||||
|
# Construire les produits avec ces atomes
|
||||||
|
co2 = CarbonDioxide(atoms_list=[carbons[0], oxygens[0], oxygens[1]])
|
||||||
|
water1 = Water(atoms_list=[hydrogens[0], hydrogens[1], oxygens[2]])
|
||||||
|
water2 = Water(atoms_list=[hydrogens[2], hydrogens[3], oxygens[3]])
|
||||||
|
|
||||||
|
return [co2, water1, water2]
|
||||||
|
|
||||||
|
molecules = [Methane(), Dioxygen(), Water(), Dioxygen()]
|
||||||
|
produits = ChemicalReaction(molecules)
|
||||||
|
print([str(m) for m in produits]) # ['CO2', 'H2O', 'H2O']
|
||||||
|
print([str(m) for m in molecules]) # ['H2O']
|
||||||
|
print(ChemicalReaction(molecules)) # None
|
||||||
Loading…
Add table
Reference in a new issue