I have written class called Atoms
and I put inside it, in short, all the information you need in order to work on distributing the electrons of some atoms, and I put it in a dictionary dict
, its symbol is the key and its atomic number is the value
, and I made a set of conditional sentences in order to distribute the atoms and put them inside a list
, but it excludes the last value and Don't put it on the list.
This is my code:
class Atoms:
def __init__(self, protons, neutrons, electrons):
self.protons = protons
self.neutrons = neutrons
self.electrons = electrons
def atoms_info(the_atom_name):
global Atoms_info
global Atomic_Number
Atoms_info = {"H": ("Hydrogen", "Atomic Number : 1"), "He": ("Helium", "Atomic Number : 2"), "Li": ("Lithium", "Atomic Number : 3"),"Be": ("Beryllium", "Atomic Number : 4"),
"B": ("Boron", "Atomic Number : 5"),"C": ("Carbon", "Atomic Number : 6"), "N": ("Nitrogen", "Atomic Number : 7"),"O": ("Oxygen", "Atomic Number : 8"),
"F": ("Fluorine", "Atomic Number : 9"), "Ne": ("Neon", "Atomic Number : 10"), "Na": ("Sodium", "Atomic Number : 11"), "Mg": ("Magnesium", "Atomic Number : 12"),
"Al": ("Aluminium", "Atomic Number : 13"), "Si": ("Silicon", "Atomic Number : 14"), "P": ("Phosphorus", "Atomic Number : 15"), "S": ("Sulfur", "Atomic Number : 16"),
"Cl": ("Chlorine", "Atomic Number : 17"), "Ar": ("Argon", "Atomic Number : 18"), "K": ("Potassium", "Atomic Number : 19"), "Ca": ("Calcium", "Atomic Number : 20"),
"Sc": ("Scandium", "Atomic Number : 21"), "Ti": ("Titanium", "Atomic Number : 22"), "V": ("Vanadium", "Atomic Number : 23"), "Cr": ("Chromium", "Atomic Number : 24"),
"Mn": ("Manganese", "Atomic Number : 25"), "Fe": ("Iron", "Atomic Number : 26"), "Co": ("Cobalt", "Atomic Number : 27"), "Ni": ("Nickel", "Atomic Number : 28"),
"Cu": ("Copper", "Atomic Number : 29"), "Zn": ("Zinc", "Atomic Number : 30"), "Ga": ("Gallium", "Atomic Number : 31"), "Ge": ("Germanium", "Atomic Number : 32"),
"As": ("Arsenic", "Atomic Number : 33"), "Se": ("Selenium", "Atomic Number : 34"), "Br": ("Bromine", "Atomic Number : 35"), "Kr": ("Krypton", "Atomic Number : 36"),
"Rb": ("Rubidium", "Atomic Number : 37"), "Sr": ("Strontium", "Atomic Number : 38"), "Y": ("Yttrium", "Atomic Number : 39"), "Zr": ("Zirconium", "Atomic Number : 40"),
"Nb": ("Niobium", "Atomic Number : 41"), "Mo": ("Molybdenum", "Atomic Number : 42"), "Tc": ("Technetium", "Atomic Number : 43"), "Ru": ("Ruthenium", "Atomic Number : 44"),
"Rh": ("Rhodium", "Atomic Number : 45"), "Pd": ("Palladium", "Atomic Number : 46"), "Ag": ("Silver", "Atomic Number : 47"), "Cd": ("Cadmium", "Atomic Number : 48"),
"In": ("Indium", "Atomic Number : 49"), "Sn": ("Tin", "Atomic Number : 50"), "Sb": ("Antimony", "Atomic Number : 51"), "Te": ("Tellurium", "Atomic Number : 52"),
"I": ("Iodine", "Atomic Number : 53"), "Xe": ("Xenon", "Atomic Number : 54"), "Cs": ("Caesium", "Atomic Number : 55"), "Ba": ("Barium", "Atomic Number : 56"),
"La": ("Lanthanum", "Atomic Number : 57"), "Ce": ("Cerium", "Atomic Number : 58"), "Pr": ("Praseodymium", "Atomic Number : 59"), "Nd": ("Neodymium", "Atomic Number : 60"),
"Pm": ("Promethium", "Atomic Number : 61"), "Sm": ("Samarium", "Atomic Number : 62"), "Eu": ("Europium", "Atomic Number : 63"), "Gd": ("Gadolinium", "Atomic Number : 64"),
"Tb": ("Terbium", "Atomic Number : 65"), "Dy": ("Dysprosium", "Atomic Number : 66"), "Ho": ("Holmium", "Atomic Number : 67"), "Er": ("Erbium", "Atomic Number : 68"),
"Tm": ("Thulium", "Atomic Number : 69"), "Yb": ("Ytterbium", "Atomic Number : 70"), "Lu": ("Lutetium", "Atomic Number : 71"), "Hf": ("Hafnium", "Atomic Number : 72"),
"Ta": ("Tantalum", "Atomic Number : 73"), "W": ("Tungsten", "Atomic Number : 74"), "Re": ("Rhenium", "Atomic Number : 75"), "Os": ("Osmium", "Atomic Number : 76"),
"Ir": ("Iridium", "Atomic Number : 77"), "Pt": ("Platinum", "Atomic Number : 78"), "Au": ("Gold", "Atomic Number : 79"), "Hg": ("Mercury", "Atomic Number : 80"),
"Tl": ("Thallium", "Atomic Number : 81"), "Pb": ("Lead", "Atomic Number : 82"), "Bi": ("Bismuth", "Atomic Number : 83"), "Po": ("Polonium", "Atomic Number : 84"),
"At": ("Astatine", "Atomic Number : 85"), "Rn": ("Radon", "Atomic Number : 86"), "Fr": ("Francium", "Atomic Number : 87"), "Ra": ("Radium", "Atomic Number : 88"),
"Ac": ("Actinium", "Atomic Number : 89"), "Th": ("Thorium", "Atomic Number : 90"), "Pa": ("Protactinium", "Atomic Number : 91"), "U": ("Uranium", "Atomic Number : 92"),
"Np": ("Neptunium", "Atomic Number : 93"), "Pu": ("Plutonium", "Atomic Number : 94"), "Am": ("Americium", "Atomic Number : 95"), "Cm": ("Curium", "Atomic Number : 96"),
"Bk": ("Berkelium", "Atomic Number : 97"), "Cf": ("Californium", "Atomic Number : 98"), "Es": ("Einsteinium", "Atomic Number : 99"), "Fm": ("Fermium", "Atomic Number : 100"),
"Md": ("Mendelevium", "Atomic Number : bitcoin101"), "No": ("Nobelium", "Atomic Number : 102"), "Lr": ("Lawrencium", "Atomic Number : 103"), "Rf": ("Rutherfordium", "Atomic Number : 104"),
"Db": ("Dubnium", "Atomic Number : 105"), "Sg": ("Seaborgium", "Atomic Number : 106"), "Bh": ("Bohrium", "Atomic Number : 107"), "Hs": ("Hassium", "Atomic Number : 108"),
"Mt": ("Meitnerium", "Atomic Number : 109"), "Ds": ("Darmstadtium", "Atomic Number : 110"), "Rg": ("Roentgenium", "Atomic Number : 111"), "Cn": ("Copernicium", "Atomic Number : 112"),
"Nh": ("Nihonium", "Atomic Number : 113"), "Fl": ("Flerovium", "Atomic Number : 114"), "Mc": ("Moscovium", "Atomic Number : 115"), "Lv": ("Livermorium", "Atomic Number : 116"),
"Ts": ("Tennessine", "Atomic Number : 117"), "Og": ("Oganesson", "Atomic Number : 118")}
if the_atom_name == 'all_info':
for key, value in Atoms_info.items():
print(key, value)
elif the_atom_name == 'symbol':
temp = 1
for key in Atoms_info:
print(f'Key_{temp} : {key}')
temp = 1
else:
print(Atoms_info.get(the_atom_name, 'No atom has this symbol'))
Atomic_Number = {"H": 1, "He": 2, "Li": 3, "Be": 4, "B": 5, "C": 6, "N": 7, "O": 8, "F": 9, "Ne": 10,
"Na": 11, "Mg": 12, "Al": 13, "Si": 14, "P": 15, "S": 16, "Cl": 17, "Ar": 18, "K": 19, "Ca": 20,"Sc": 21, "Ti": 22,
"V": 23, "Cr": 24, "Mn": 25, "Fe": 26, "Co": 27, "Ni": 28, "Cu": 29, "Zn": 30, "Ga": 31, "Ge": 32, "As": 33, "Se": 34,
"Br": 35, "Kr": 36, "Rb": 37, "Sr": 38,"Y": 39, "Zr": 40, "Nb": 41, "Mo": 42, "Tc": 43, "Ru": 44, "Rh": 45, "Pd": 46,
"Ag": 47,"Cd": 48, "In": 49, "Sn": 50, "Sb": 51, "Te": 52, "I": 53, "Xe": 54, "Cs": 55, "Ba": 56, "La": 57, "Ce": 58,
"Pr": 59,"Nd": 60, "Pm": 61, "Sm": 62, "Eu": 63, "Gd": 64, "Tb": 65, "Dy": 66, "Ho": 67, "Er": 68, "Tm": 69, "Yb": 70,
"Lu": 71, "Hf": 72, "Ta": 73, "W": 74, "Re": 75, "Os": 76,"Ir": 77, "Pt": 78, "Au": 79, "Hg": 80, "Tl": 81, "Pb": 82,
"Bi": 83, "Po": 84, "At": 85, "Rn": 86, "Fr": 87, "Ra": 88, "Ac": 89, "Th": 90, "Pa": 91, "U": 92, "Np": 93, "Pu": 94,
"Am": 95, "Cm": 96, "Bk": 97, "Cf": 98, "Es": 99, "Fm": 100, "Md": 101, "No": 102, "Lr": 103, "Rf": 104, "Db": 105,
"Sg": 106, "Bh": 107, "Hs": 108, "Mt": 109, "Ds": 110, "Rg": 111, "Cn": 112, "Nh": 113, "Fl": 114, "Mc": 115, "Lv": 116,
"Ts": 117, "Og": 118}
@classmethod
def e_orbit(self, element):
self.element = element
Atomic_Number = {"H": 1, "He": 2, "Li": 3, "Be": 4, "B": 5, "C": 6, "N": 7, "O": 8, "F": 9, "Ne": 10,
"Na": 11, "Mg": 12, "Al": 13, "Si": 14, "P": 15, "S": 16, "Cl": 17, "Ar": 18, "K": 19, "Ca": 20,"Sc": 21, "Ti": 22,
"V": 23, "Cr": 24, "Mn": 25, "Fe": 26, "Co": 27, "Ni": 28, "Cu": 29, "Zn": 30, "Ga": 31, "Ge": 32, "As": 33, "Se": 34,
"Br": 35, "Kr": 36, "Rb": 37, "Sr": 38,"Y": 39, "Zr": 40, "Nb": 41, "Mo": 42, "Tc": 43, "Ru": 44, "Rh": 45, "Pd": 46,
"Ag": 47,"Cd": 48, "In": 49, "Sn": 50, "Sb": 51, "Te": 52, "I": 53, "Xe": 54, "Cs": 55, "Ba": 56, "La": 57, "Ce": 58,
"Pr": 59,"Nd": 60, "Pm": 61, "Sm": 62, "Eu": 63, "Gd": 64, "Tb": 65, "Dy": 66, "Ho": 67, "Er": 68, "Tm": 69, "Yb": 70,
"Lu": 71, "Hf": 72, "Ta": 73, "W": 74, "Re": 75, "Os": 76,"Ir": 77, "Pt": 78, "Au": 79, "Hg": 80, "Tl": 81, "Pb": 82,
"Bi": 83, "Po": 84, "At": 85, "Rn": 86, "Fr": 87, "Ra": 88, "Ac": 89, "Th": 90, "Pa": 91, "U": 92, "Np": 93, "Pu": 94,
"Am": 95, "Cm": 96, "Bk": 97, "Cf": 98, "Es": 99, "Fm": 100, "Md": 101, "No": 102, "Lr": 103, "Rf": 104, "Db": 105,
"Sg": 106, "Bh": 107, "Hs": 108, "Mt": 109, "Ds": 110, "Rg": 111, "Cn": 112, "Nh": 113, "Fl": 114, "Mc": 115, "Lv": 116,
"Ts": 117, "Og": 118}
# the problem
e_dist = []
Atomic_number_tmp = Atomic_Number[element]
while Atomic_number_tmp != 0:
if Atomic_number_tmp == 1:
e_dist.append(1)
break
else:
e_dist.append(2)
Atomic_number_tmp - 2
if Atomic_number_tmp <= 0:
break
else:
if (Atomic_number_tmp - 8) == 0:
e_dist.append(8)
break
elif (Atomic_number_tmp - 8) > 0:
e_dist.append(8)
Atomic_number_tmp - 8
if Atomic_number_tmp <= 0:
break
elif Atomic_number_tmp > 0 and Atomic_number_tmp <= 7:
e_dist.append(Atomic_number_tmp)
elif (Atomic_number_tmp - 8) < 0:
break
break
print(e_dist)
# TEST FUNCTION
Atoms.e_orbit("Na")
The it's return [2, 8]
, But it must return [2, 8, 1]
I tried to read the code to understand the exact problem, but it is very complicated, I hope anybody can help my to solve this problem and make my code more readable.
Thank you for reading.
CodePudding user response:
The following code is my int4erpretation of your code.
- global statements should be avoided at all cost.
You can make your attributs class atributes by adding them drictly to the class. It fullfills the same purpuse but it is much clearer and your functions loose code and are easyier to read. Thats just style thouhg. I NEVER use
global
. - You do not asing your changes.
variable -1
does not change the variable tryvariable -= 1
. - You assinged your one twice. I assume by mistake.
- I reduced the code just to get a bit of overview.
- Statichmethod something similar as classmethod. But i am unsure about that. Please note that in your code the classmethod decorator is not at the right place. One whitespace is missing.
class Atoms:
ATOMIC_NUMBER = {"H": 1, "He": 2, "Li": 3, "Be": 4, "B": 5, "C": 6, "N": 7, "O": 8, "F": 9, "Ne": 10,
"Na": 11, "Mg": 12, "Al": 13, "Si": 14, "P": 15, "S": 16, "Cl": 17, "Ar": 18, "K": 19, "Ca": 20,"Sc": 21, "Ti": 22,
"V": 23, "Cr": 24, "Mn": 25, "Fe": 26, "Co": 27, "Ni": 28, "Cu": 29, "Zn": 30, "Ga": 31, "Ge": 32, "As": 33, "Se": 34,
"Br": 35, "Kr": 36, "Rb": 37, "Sr": 38,"Y": 39, "Zr": 40, "Nb": 41, "Mo": 42, "Tc": 43, "Ru": 44, "Rh": 45, "Pd": 46,
"Ag": 47,"Cd": 48, "In": 49, "Sn": 50, "Sb": 51, "Te": 52, "I": 53, "Xe": 54, "Cs": 55, "Ba": 56, "La": 57, "Ce": 58,
"Pr": 59,"Nd": 60, "Pm": 61, "Sm": 62, "Eu": 63, "Gd": 64, "Tb": 65, "Dy": 66, "Ho": 67, "Er": 68, "Tm": 69, "Yb": 70,
"Lu": 71, "Hf": 72, "Ta": 73, "W": 74, "Re": 75, "Os": 76,"Ir": 77, "Pt": 78, "Au": 79, "Hg": 80, "Tl": 81, "Pb": 82,
"Bi": 83, "Po": 84, "At": 85, "Rn": 86, "Fr": 87, "Ra": 88, "Ac": 89, "Th": 90, "Pa": 91, "U": 92, "Np": 93, "Pu": 94,
"Am": 95, "Cm": 96, "Bk": 97, "Cf": 98, "Es": 99, "Fm": 100, "Md": 101, "No": 102, "Lr": 103, "Rf": 104, "Db": 105,
"Sg": 106, "Bh": 107, "Hs": 108, "Mt": 109, "Ds": 110, "Rg": 111, "Cn": 112, "Nh": 113, "Fl": 114, "Mc": 115, "Lv": 116,
"Ts": 117, "Og": 118}
ATOM_INFO = {"H": ("Hydrogen", "Atomic Number : 1"), "He": ("Helium", "Atomic Number : 2"), "Li": ("Lithium", "Atomic Number : 3"),"Be": ("Beryllium", "Atomic Number : 4"),
"B": ("Boron", "Atomic Number : 5"),"C": ("Carbon", "Atomic Number : 6"), "N": ("Nitrogen", "Atomic Number : 7"),"O": ("Oxygen", "Atomic Number : 8"),
"F": ("Fluorine", "Atomic Number : 9"), "Ne": ("Neon", "Atomic Number : 10"), "Na": ("Sodium", "Atomic Number : 11"), "Mg": ("Magnesium", "Atomic Number : 12"),
"Al": ("Aluminium", "Atomic Number : 13"), "Si": ("Silicon", "Atomic Number : 14"), "P": ("Phosphorus", "Atomic Number : 15"), "S": ("Sulfur", "Atomic Number : 16"),
"Cl": ("Chlorine", "Atomic Number : 17"), "Ar": ("Argon", "Atomic Number : 18"), "K": ("Potassium", "Atomic Number : 19"), "Ca": ("Calcium", "Atomic Number : 20"),
"Sc": ("Scandium", "Atomic Number : 21"), "Ti": ("Titanium", "Atomic Number : 22"), "V": ("Vanadium", "Atomic Number : 23"), "Cr": ("Chromium", "Atomic Number : 24"),
"Mn": ("Manganese", "Atomic Number : 25"), "Fe": ("Iron", "Atomic Number : 26"), "Co": ("Cobalt", "Atomic Number : 27"), "Ni": ("Nickel", "Atomic Number : 28"),
"Cu": ("Copper", "Atomic Number : 29"), "Zn": ("Zinc", "Atomic Number : 30"), "Ga": ("Gallium", "Atomic Number : 31"), "Ge": ("Germanium", "Atomic Number : 32"),
"As": ("Arsenic", "Atomic Number : 33"), "Se": ("Selenium", "Atomic Number : 34"), "Br": ("Bromine", "Atomic Number : 35"), "Kr": ("Krypton", "Atomic Number : 36"),
"Rb": ("Rubidium", "Atomic Number : 37"), "Sr": ("Strontium", "Atomic Number : 38"), "Y": ("Yttrium", "Atomic Number : 39"), "Zr": ("Zirconium", "Atomic Number : 40"),
"Nb": ("Niobium", "Atomic Number : 41"), "Mo": ("Molybdenum", "Atomic Number : 42"), "Tc": ("Technetium", "Atomic Number : 43"), "Ru": ("Ruthenium", "Atomic Number : 44"),
"Rh": ("Rhodium", "Atomic Number : 45"), "Pd": ("Palladium", "Atomic Number : 46"), "Ag": ("Silver", "Atomic Number : 47"), "Cd": ("Cadmium", "Atomic Number : 48"),
"In": ("Indium", "Atomic Number : 49"), "Sn": ("Tin", "Atomic Number : 50"), "Sb": ("Antimony", "Atomic Number : 51"), "Te": ("Tellurium", "Atomic Number : 52"),
"I": ("Iodine", "Atomic Number : 53"), "Xe": ("Xenon", "Atomic Number : 54"), "Cs": ("Caesium", "Atomic Number : 55"), "Ba": ("Barium", "Atomic Number : 56"),
"La": ("Lanthanum", "Atomic Number : 57"), "Ce": ("Cerium", "Atomic Number : 58"), "Pr": ("Praseodymium", "Atomic Number : 59"), "Nd": ("Neodymium", "Atomic Number : 60"),
"Pm": ("Promethium", "Atomic Number : 61"), "Sm": ("Samarium", "Atomic Number : 62"), "Eu": ("Europium", "Atomic Number : 63"), "Gd": ("Gadolinium", "Atomic Number : 64"),
"Tb": ("Terbium", "Atomic Number : 65"), "Dy": ("Dysprosium", "Atomic Number : 66"), "Ho": ("Holmium", "Atomic Number : 67"), "Er": ("Erbium", "Atomic Number : 68"),
"Tm": ("Thulium", "Atomic Number : 69"), "Yb": ("Ytterbium", "Atomic Number : 70"), "Lu": ("Lutetium", "Atomic Number : 71"), "Hf": ("Hafnium", "Atomic Number : 72"),
"Ta": ("Tantalum", "Atomic Number : 73"), "W": ("Tungsten", "Atomic Number : 74"), "Re": ("Rhenium", "Atomic Number : 75"), "Os": ("Osmium", "Atomic Number : 76"),
"Ir": ("Iridium", "Atomic Number : 77"), "Pt": ("Platinum", "Atomic Number : 78"), "Au": ("Gold", "Atomic Number : 79"), "Hg": ("Mercury", "Atomic Number : 80"),
"Tl": ("Thallium", "Atomic Number : 81"), "Pb": ("Lead", "Atomic Number : 82"), "Bi": ("Bismuth", "Atomic Number : 83"), "Po": ("Polonium", "Atomic Number : 84"),
"At": ("Astatine", "Atomic Number : 85"), "Rn": ("Radon", "Atomic Number : 86"), "Fr": ("Francium", "Atomic Number : 87"), "Ra": ("Radium", "Atomic Number : 88"),
"Ac": ("Actinium", "Atomic Number : 89"), "Th": ("Thorium", "Atomic Number : 90"), "Pa": ("Protactinium", "Atomic Number : 91"), "U": ("Uranium", "Atomic Number : 92"),
"Np": ("Neptunium", "Atomic Number : 93"), "Pu": ("Plutonium", "Atomic Number : 94"), "Am": ("Americium", "Atomic Number : 95"), "Cm": ("Curium", "Atomic Number : 96"),
"Bk": ("Berkelium", "Atomic Number : 97"), "Cf": ("Californium", "Atomic Number : 98"), "Es": ("Einsteinium", "Atomic Number : 99"), "Fm": ("Fermium", "Atomic Number : 100"),
"Md": ("Mendelevium", "Atomic Number : bitcoin101"), "No": ("Nobelium", "Atomic Number : 102"), "Lr": ("Lawrencium", "Atomic Number : 103"), "Rf": ("Rutherfordium", "Atomic Number : 104"),
"Db": ("Dubnium", "Atomic Number : 105"), "Sg": ("Seaborgium", "Atomic Number : 106"), "Bh": ("Bohrium", "Atomic Number : 107"), "Hs": ("Hassium", "Atomic Number : 108"),
"Mt": ("Meitnerium", "Atomic Number : 109"), "Ds": ("Darmstadtium", "Atomic Number : 110"), "Rg": ("Roentgenium", "Atomic Number : 111"), "Cn": ("Copernicium", "Atomic Number : 112"),
"Nh": ("Nihonium", "Atomic Number : 113"), "Fl": ("Flerovium", "Atomic Number : 114"), "Mc": ("Moscovium", "Atomic Number : 115"), "Lv": ("Livermorium", "Atomic Number : 116"),
"Ts": ("Tennessine", "Atomic Number : 117"), "Og": ("Oganesson", "Atomic Number : 118")}
def __init__(self, protons, neutrons, electrons):
self.protons = protons
self.neutrons = neutrons
self.electrons = electrons
def atoms_info(the_atom_name):
if the_atom_name == 'all_info':
for key, value in Atoms.ATOM_INFO.items():
print(key, value)
elif the_atom_name == 'symbol':
temp = 1
for key in Atoms.ATOM_INFO:
print(f'Key_{temp} : {key}')
temp = 1
else:
print(Atoms.ATOM_INFO.get(the_atom_name, 'No atom has this symbol'))
@staticmethod
def e_orbit(element):
element = element
# the problem
e_dist = []
Atomic_number_tmp = Atoms.ATOMIC_NUMBER[element]
while Atomic_number_tmp > 0:
if Atomic_number_tmp == 1:
Atomic_number_tmp -= 1
e_dist.append(1)
break
else:
e_dist.append(2)
Atomic_number_tmp -= 2
if Atomic_number_tmp > 0:
if (Atomic_number_tmp - 8) == 0:
e_dist.append(8)
elif (Atomic_number_tmp - 8) > 0:
e_dist.append(8)
Atomic_number_tmp -= 8
if Atomic_number_tmp > 1 and Atomic_number_tmp <= 8:
# second append for one was here i am nit clear if that was correct
e_dist.append(Atomic_number_tmp)
elif (Atomic_number_tmp - 8) < 0:
continue
# TEST FUNCTION
Atoms.e_orbit("Na")
CodePudding user response:
while condition should be like this if you want to num of loop is equal to num of item
while Atomic_number_tmp < 0: