Molécules
This commit is contained in:
parent
f7212eb184
commit
32cd698d35
1 changed files with 36 additions and 0 deletions
36
main.py
36
main.py
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue