4
\$\begingroup\$

This code is a digital periodic table that I am working on, but it is taking forever as it is very long and I would like to shorten it.

#This is the intro sentence that explains the way my element index works.
print("Welcome to my element index! You must enter the name of an element with a capital letter. Also you are able to type a capital letter to see lists of elements beginning with that letter. Any element with N/A next to it is not in the index at the moment. To see all of the letter lists you have to type the letters in alphabetical order. For example if you want to see F and after you want to see A, you have to restart the program to see A. And if you want to see Fluorine and then after Hydrogen, you have to restart the program to see Hydrogen.")

#This is the variable that the user inputs the element they want to find or the letter to find an element.
input_ = input("Please enter the name or first capital letter of an element:")

#This very big chunk of code is the entire alphabetical list system. It checks what letter the input_ variable has and displays the list of that letter.
if input_ == "A":
    print("Elements beginning with the letter A: Aluminium  Argon  Arsenic  Astatine(N/A)  Americium(N/A)  Actinium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "B":
    print("Elements beginning with the letter B: Berylium  Boron  Bromine  Bismuth(N/A)  Bohrium(N/A")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "C":
    print("Elements beginning with the letter C: Carbon  Chlorine  Calcium  Chromium  Cobalt  Copper  Cadmium  Caesium(N/A)  Copernicium(N/A)  Cerium(N/A)  Curium(N/A)  Californium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "D":
    print("Elements beginning with the letter D: Dubnium(N/A)  Darmstadtium(N/A)  Dysprosium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "E":
    print("Elements beginning with the letter E: Europium(N/A)  Einsteinium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "F":
    print("Elements beginning with the letter F: Fluorine  Francium(N/A)  Fermium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "G":
    print("Elements beginning with the letter G: Gallium  Germanium  Gold(N/A)  Gadolinium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "H":
    print("Elements beginning with the letter H: Hydrogen  Helium  Hafnium(N/A)  Hassium(N/A)  Holmium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "I":
    print("Elements beginning with the letter I: Iron  Iodine  Iridium(N/A)  Indium")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "J":
    print("There are no elements beginning with the letter J")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "K":
    print("Elements beginning with the letter K: Krypton")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "L":
    print("Elements beginning with the letter L: Lithium  Lead(N/A)  Lanthanum(N/A)  Lutetium(N/A)  Lawrencium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "M":
    print("Elements beginning with the letter M: Magnesium  Magnanese  Molybdenum  Meitnerium(N/A)  Mendelevium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "N":
    print("Elements beginning with the letter N: Nitrogen  Neon  Nickel  Niobium  Neodymium(N/A)  Nobelium(N/A)  Neptunium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "O":
    print("Elements beginning with the letter O: Oxygen  Osmium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "P":
    print("Elements beginning with the letter P: Potassium  Phosphorus  Platinum(N/A)  Plutonium(N/A)  Promethium(N/A)  Protactinium(N/A)  Praseodymium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "Q":
    print("There are no elements beginning with the letter Q")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "R":
    print("Elements beginning with the letter R: Rubidium  Ruthenium(N/A)  Rhodium(N/A)  Radon(N/A)  Rhenium(N/A)  Radium(N/A)  Rutherfordium(N/A)  Roentgenium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "S":
    print("Elements beginning with the letter S: Sodium  Silicon  Sulphur  Scandium  Selenium  Strontium  Silver  Seaborgium(N/A)  Samarium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "T":
    print("Elements beginning with the letter T: Titanium  Technetium  Tin  Tellurium(N/A)  Tantalum(N/A)  Tungsten(N/A)  Thallium(N/A)  Terbium(N/A)  Thulium(N/A)  Thorium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "U":
    print("Elements beginning with the letter U: Urainium(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "V":
    print("Elements beginning with the letter V: Vanadium")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "W":
    print("There are no elements beginning with the letter W")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "X":
    print("Elements beginning with the letter X: Xenon(N/A)")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "Y":
    print("Elements beginning with the letter Y: Yttrium  Ytterbium(N/A")
    input_ = input("Please enter the name or first capital letter of an element:")

elif input_ == "Z":
    print("Elements beginning with the letter Z: Zirconium")
    input_ = input("Please enter the name or first capital letter of an element:")

#This chunk of code is the actual elements that you search up. This time, it checks what element the input_ variable has and displays the information of that element.
if input_ == "Hydrogen":
    print("Element: Hydrogen  Symbol: H  Atomic Number: 1  Atomic Mass: 1  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Helium":
    print("Element: Helium  Symbol: He  Atomic Number: 2  Atomic Mass: 4  Group: Noble Gases")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Lithium":
    print("Element: Lithium  Symbol: Li  Atomic Number: 3  Atomic Mass: 7  Group: Alkali Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Berylium":
    print("Element: Berylium  Symbol: Be  Atomic Number: 4  Atomic Mass: 9  Group: Alkaline Earth Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Boron":
    print("Element: Boron  Symbol: B  Atomic Number: 5  Atomic Mass: 11  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Carbon":
    print("Element: Carbon  Symbol: C  Atomic Number: 6  Atomic Mass: 12  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Nitrogen":
    print("Element: Nitrogen  Symbol: N  Atomic Number: 7  Atomic Mass: 14  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Oxygen":
    print("Element: Oxygen  Symbol: O  Atomic Number: 8  Atomic Mass: 16  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Fluorine":
    print("Element: Fluorine  Symbol: F Atomic Number: 9  Atomic Mass: 19  Group: Halogens")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Neon":
    print("Element: Neon  Symbol: Ne  Atomic Number: 10  Atomic Mass: 20  Group: Noble Gasses")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Sodium":
    print("Element: Sodium  Symbol: Na  Atomic Number: 11  Atomic Mass: 23  Group: Alkali Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Magnesium":
    print("Element: Magnesium  Symbol: Mg  Atomic Number: 12  Atomic Mass: 24  Group: Alkaline Earth Metal")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Aluminium":
    print("Element: Aluminium  Symbol: Al  Atomic Number: 13  Atomic Mass: 27  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element")

if input_ == "Silicon":
    print("Element: Silicon  Symbol: Si  Atomic Number: 14  Atomic Mass: 28  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Phosphorus":
    print("Element: Phosphorus  Symbol: P  Atomic Number: 15  Atomic Mass: 31  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Sulphur":
    print("Element: Sulphur  Symbol: S  Atomic Number: 16  Atomic Mass: 32  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Chlorine":
    print("Element: Chlorine  Symbol: Cl  Atomic Number: 17  Atomic Mass: 35.5  Group: Halogens")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Argon":
    print("Element: Argon  Symbol: Ar  Atomic Number: 18  Atomic Mass: 40  Group: Noble Gasses")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Potassium":
    print("Element: Potassium  Symbol: K  Atomic Number: 19  Atomic Mass: 39  Group: Alkali Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Calcium":
    print("Element: Calcium  Symbol: Ca  Atomic Number: 20  Atomic Mass: 40  Group: Alkaline Earth Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Scandium":
    print("Element: Scandium  Symbol: Sc  Atomic Number: 21  Atomic Mass: 45  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Titanium":
    print("Element: Titanium  Symbol: Ti  Atomic Number: 22  Atomic Mass: 48  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Vanadium":
    print("Element: Vanadium  Symbol: V  Atomic Number: 23  Atomic Mass: 51  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Chromium":
    print("Element: Chromium  Symbol: Cr  Atomic Number: 24  Atomic Mass: 52  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Manganese":
    print("Element: Manganese  Symbol: Mn  Atomic Number: 25  Atomic Mass: 55  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Iron":
    print("Element: Iron  Symbol: Fe  Atomic Number: 26  Atomic Mass: 56  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Cobalt":
    print("Element: Cobalt  Symbol: Co  Atomic Number: 27  Atomic Mass: 59  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Nickel":
    print("Element: Nickel  Symbol: Ni  Atomic Number: 28  Atomic Mass: 59  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Copper":
    print("Element: Copper  Symbol: Cu  Atomic Number: 29  Atomic Mass: 63.5  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Zinc":
    print("Element: Zinc  Symbol: Zn  Atomic Number: 30  Atomic Mass: 65  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Gallium":
    print("Element: Gallium  Symbol: Ga  Atomic Number: 31  Atomic Mass: 70  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Germanium":
    print("Element: Germanium  Symbol: Ge  Atomic Number: 32  Atomic Mass: 73  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Arsenic":
    print("Element: Arsenic  Symbol: As  Atomic Number: 33  Atomic Mass: 75  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Selenium":
    print("Element: Selenium  Symbol: Se  Atomic Number: 34  Atomic Mass: 79  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Bromine":
    print("Element: Bromine  Symbol: Br  Atomic Number: 35  Atomic Mass: 80  Group: Halogens")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Krypton":
    print("Element: Krypton  Symbol: Kr  Atomic Number: 36  Atomic Mass: 84  Group: Noble Gasses")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Rubidium":
    print("Element: Rubidium  Symbol: Rb  Atomic Number: 37  Atomic Mass: 85  Group: Alkali Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Strontium":
    print("Element: Strontium  Symbol: Sr  Atomic Number: 38  Atomic Mass: 88  Group: Alkaline Earth Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Yttrium":
    print("Element: Yttrium  Symbol: Y  Atomic Number: 39  Atomic Mass: 89  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Zirconium":
    print("Element: Zirconium  Symbol: Zr  Atomic Number: 40  Atomic Mass: 91  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Niobium":
    print("Element: Niobium  Symbol: Nb  Atomic Number: 41  Atomic Mass: 93  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Molybdenum":
    print("Element: Molybdenum  Symbol: Mo  Atomic Number: 42  Atomic Mass: 96  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Technetium":
    print("Element: Technetium  Symbol: Tc  Atomic Number: 43  Atomic Mass: 98  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Ruthenium":
    print("Element: Ruthenium  Symbol: Ru  Atomic Number: 44  Atomic Mass: 101  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Rhodium":
    print("Element: Rhodium  Symbol: Rh  Atomic Number: 45  Atomic Mass: 103  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Palladium":
    print("Element: Palladium  Symbol: Pd  Atomic Number: 46  Atomic Mass: 106  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Silver":
    print("Element: Silver  Symbol: Ag  Atomic Number: 47  Atomic Mass: 108  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Cadmium":
    print("Element: Cadmium  Symbol: Cd  Atomic Number: 48  Atomic Mass: 112  Group: Transition Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Indium":
    print("Element: Indium  Symbol: In  Atomic Number: 49  Atomic Mass: 115  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Tin":
    print("Element: Tin  Symbol: Sn  Atomic Number: 50  Atomic Mass: 119  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Antimony":
    print("Element: Antimony  Symbol: Sb  Atomic Number: 51  Atomic Mass: 122  Group: Other Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Tellurium":
    print("Element: Tellurium  Symbol: Te  Atomic Number: 52  Atomic Mass: 128  Group: Non Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Iodine":
    print("Element: Iodine  Symbol: I  Atomic Number: 53  Atomic Mass: 127  Group: Halogens")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Xenon":
    print("Element: Xenon  Symbol: Xe  Atomic Number: 54  Atomic Mass: 131  Group: Noble Gasses")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Caesium":
    print("Element: Caesium  Symbol: Cs  Atomic Number: 55  Atomic Mass: 133  Group: Alkali Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Barium":
    print("Element: Barium  Symbol: Ba  Atomic Number: 56  Atomic Mass: 137  Group: Alkaline Earth Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Lanthanum":
    print("Element: Lanthanum  Symbol: La  Atomic Number: 57  Atomic Mass: 139  Group: Rare Earth Metals")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Cerium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")


if input_ == "Praseodymium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Neodymium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Promethium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Samarium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Europium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Gadolinium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Terbium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Dysprosium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Holmium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Erbium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Thulium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Ytterbium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Lutetium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Hafnium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Tantalum":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Tungsten":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")

if input_ == "Rhenium":
    print("")
    input_ = input("Please enter the name or first capital letter of an element:")
\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

You're writing out all the possible lookup results as code, using if-else statements. What you should be doing is putting the information into a data structure (namely a dictionary), and writing code to present the results of searching the dictionary.

As a result of your manual coding, you've made a few mistakes:

  • Some entries are missing. For example, your code says that 6 elements start with "A", but you omitted antimony. Similarly, you omitted palladium under "P".
  • Beryllium and uranium are misspelled.
  • In a couple of places (e.g. bohrium), you neglected to close the parenthesis.

Suggested solution

As mentioned above, you should put all of the data into a data structure. I suggest using a dictionary, where each value is a collections.namedtuple that contains the symbol, atomic number, atomic mass, and group. For the elements where we don't have the data, we can use None as a placeholder value.

After that, it's mainly an issue of how to display the results. There are four possible outcomes:

  • The user-specified lookup is a key in the dictionary, and the value is not None. Then we print the data. I suggest using str.format().
  • The user-specified lookup is a key in the dictionary, but the value is None. You currently print an empty line, but it should be more informative.
  • No element has a name that starts with the lookup value.
  • Some element(s) have a name that starts with the lookup value. Reporting the list of elements is a bit tricky: I've used ' '.join(…) with a generator expression.

Of course, the program would actually be simpler if you simply filled in all of the missing data, so that you don't have to handle the N/A cases.

from collections import namedtuple

Element = namedtuple('Element', 'symbol atomic_number atomic_mass group')

ELEMENTS = {
    'Hydrogen': Element('H', 1, 1, 'Non Metals'),
    'Helium': Element('He', 2, 4, 'Noble Gases'),
    'Lithium': Element('Li', 3, 7, 'Alkali Metals'),
    'Berylium': Element('Be', 4, 9, 'Alkaline Earth Metals'),
    'Boron': Element('B', 5, 11, 'Non Metals'),
    'Carbon': Element('C', 6, 12, 'Non Metals'),
    'Nitrogen': Element('N', 7, 14, 'Non Metals'),
    'Oxygen': Element('O', 8, 16, 'Non Metals'),
    'Fluorine': Element('F', 9, 19, 'Halogens'),
    'Neon': Element('Ne', 10, 20, 'Noble Gasses'),
    'Sodium': Element('Na', 11, 23, 'Alkali Metals'),
    'Magnesium': Element('Mg', 12, 24, 'Alkaline Earth Metal'),
    'Aluminium': Element('Al', 13, 27, 'Other Metals'),
    'Silicon': Element('Si', 14, 28, 'Non Metals'),
    'Phosphorus': Element('P', 15, 31, 'Non Metals'),
    'Sulphur': Element('S', 16, 32, 'Non Metals'),
    'Chlorine': Element('Cl', 17, 35.5, 'Halogens'),
    'Argon': Element('Ar', 18, 40, 'Noble Gasses'),
    'Potassium': Element('K', 19, 39, 'Alkali Metals'),
    'Calcium': Element('Ca', 20, 40, 'Alkaline Earth Metals'),
    'Scandium': Element('Sc', 21, 45, 'Transition Metals'),
    'Titanium': Element('Ti', 22, 48, 'Transition Metals'),
    'Vanadium': Element('V', 23, 51, 'Transition Metals'),
    'Chromium': Element('Cr', 24, 52, 'Transition Metals'),
    'Manganese': Element('Mn', 25, 55, 'Transition Metals'),
    'Iron': Element('Fe', 26, 56, 'Transition Metals'),
    'Cobalt': Element('Co', 27, 59, 'Transition Metals'),
    'Nickel': Element('Ni', 28, 59, 'Transition Metals'),
    'Copper': Element('Cu', 29, 63.5, 'Transition Metals'),
    'Zinc': Element('Zn', 30, 65, 'Transition Metals'),
    'Gallium': Element('Ga', 31, 70, 'Other Metals'),
    'Germanium': Element('Ge', 32, 73, 'Other Metals'),
    'Arsenic': Element('As', 33, 75, 'Non Metals'),
    'Selenium': Element('Se', 34, 79, 'Non Metals'),
    'Bromine': Element('Br', 35, 80, 'Halogens'),
    'Krypton': Element('Kr', 36, 84, 'Noble Gasses'),
    'Rubidium': Element('Rb', 37, 85, 'Alkali Metals'),
    'Strontium': Element('Sr', 38, 88, 'Alkaline Earth Metals'),
    'Yttrium': Element('Y', 39, 89, 'Transition Metals'),
    'Zirconium': Element('Zr', 40, 91, 'Transition Metals'),
    'Niobium': Element('Nb', 41, 93, 'Transition Metals'),
    'Molybdenum': Element('Mo', 42, 96, 'Transition Metals'),
    'Technetium': Element('Tc', 43, 98, 'Transition Metals'),
    'Ruthenium': Element('Ru', 44, 101, 'Transition Metals'),
    'Rhodium': Element('Rh', 45, 103, 'Transition Metals'),
    'Palladium': Element('Pd', 46, 106, 'Transition Metals'),
    'Silver': Element('Ag', 47, 108, 'Transition Metals'),
    'Cadmium': Element('Cd', 48, 112, 'Transition Metals'),
    'Indium': Element('In', 49, 115, 'Other Metals'),
    'Tin': Element('Sn', 50, 119, 'Other Metals'),
    'Antimony': Element('Sb', 51, 122, 'Other Metals'),
    'Tellurium': Element('Te', 52, 128, 'Non Metals'),
    'Iodine': Element('I', 53, 127, 'Halogens'),
    'Xenon': Element('Xe', 54, 131, 'Noble Gasses'),
    'Caesium': Element('Cs', 55, 133, 'Alkali Metals'),
    'Barium': Element('Ba', 56, 137, 'Alkaline Earth Metals'),
    'Lanthanum': Element('La', 57, 139, 'Rare Earth Metals'),

    # Elements for which we currently lack data:
    'Astatine': None,
    'Americium': None,
    'Actinium': None,
    'Bismuth': None,
    'Bohrium': None,
    'Caesium': None,
    'Copernicium': None,
    'Cerium': None,
    'Curium': None,
    'Californium': None,
    'Dubnium': None,
    'Darmstadtium': None,
    'Dysprosium': None,
    'Europium': None,
    'Einsteinium': None,
    'Francium': None,
    'Fermium': None,
    'Gold': None,
    'Gadolinium': None,
    'Hafnium': None,
    'Hassium': None,
    'Holmium': None,
    'Iridium': None,
    'Lead': None,
    'Lanthanum': None,
    'Lutetium': None,
    'Lawrencium': None,
    'Meitnerium': None,
    'Mendelevium': None,
    'Neodymium': None,
    'Nobelium': None,
    'Neptunium': None,
    'Osmium': None,
    'Platinum': None,
    'Plutonium': None,
    'Promethium': None,
    'Protactinium': None,
    'Praseodymium': None,
    'Ruthenium': None,
    'Rhodium': None,
    'Radon': None,
    'Rhenium': None,
    'Radium': None,
    'Rutherfordium': None,
    'Roentgenium': None,
    'Seaborgium': None,
    'Samarium': None,
    'Tellurium': None,
    'Tantalum': None,
    'Tungsten': None,
    'Thallium': None,
    'Terbium': None,
    'Thulium': None,
    'Thorium': None,
    'Uranium': None,
    'Xenon': None,
    'Ytterbium': None,
}

print("""Welcome to my element index! You must enter the name of an element with
a capital letter. Also you are able to type a capital letter to see lists of
elements beginning with that letter. Any element with N/A next to it is not in
the index at the moment. To see all of the letter lists you have to type the
letters in alphabetical order. For example if you want to see F and after you
want to see A, you have to restart the program to see A. And if you want to see
Fluorine and then after Hydrogen, you have to restart the program to see
Hydrogen.""")

lookup = input("Please enter the name or first capital letter of an element: ")
found = [name for name in ELEMENTS if name.startswith(lookup)]

if ELEMENTS.get(lookup):
    print("Element: {name}  Symbol: {e.symbol}  Atomic Number: {e.atomic_number}  Atomic Mass: {e.atomic_mass}  Group: {e.group}".format(name=lookup, e=ELEMENTS[lookup]))
elif lookup in ELEMENTS:
    print("{name} exists, but we lack the data for it.".format(name=lookup))
elif not found:
    print("There are no elements beginning with {init}".format(init=lookup))
else:
    print(
        "Elements beginning with {init}: {list}".format(
            init=lookup,
            list='  '.join(
                '{name}{na}'.format(
                    name=name,
                    na='' if ELEMENTS[name] else '(N/A)'
                )
                for name in found
            )
        )
    )
\$\endgroup\$
5
  • \$\begingroup\$ Really a big improvement. Normally I would move the construction of found into a (nested) else clause, since it could be quite expensive, but since the size of the periodic system is more or less fixed and it takes less than 20 us on machine to construct it, it is probably fine to calculate it also when not needed. \$\endgroup\$
    – Graipher
    Commented Oct 18, 2018 at 7:34
  • \$\begingroup\$ Thanks alot 200_success you have helped me with my code and with my post \$\endgroup\$ Commented Oct 18, 2018 at 14:38
  • \$\begingroup\$ And maybe can you help me make the code loop back to allow the user to input another element after they have already checked one. \$\endgroup\$ Commented Oct 18, 2018 at 14:43
  • \$\begingroup\$ Helping you add a loop would go beyond the scope of Code Review. \$\endgroup\$ Commented Oct 18, 2018 at 16:09
  • \$\begingroup\$ It would be a good idea to upvote any posts that you find helpful, though. Votes are what keeps the community going. \$\endgroup\$ Commented Oct 18, 2018 at 19:39
0
\$\begingroup\$

Here's the thing: you should probably store all of these elements in a file of some sort (say elements.txt). The format of the file can be as follows:

Hydrogen Element: Hydrogen  Symbol: H  Atomic Number: 1  Atomic Mass: 1  Group: Non Metals
Helium Element: Helium  Symbol: He  Atomic Number: 2  Atomic Mass: 4  Group: Noble Gases
...

Now, in your Python code, you can change your code so that it reads from this file at startup i.e:

with open("elements.txt") as f:
    all_lines = f.read().splitlines()
    elements = []
    for line in all_lines:
        elements.append((line.split()[0], line[line.find(" "):].strip()))
        # Create a list of tuples with the first part the name of the element,
        # the second part its info.

Now that you have a list of elements, you can then significantly shorten the rest of your code to something that uses a loop to iterate over your elements in order to determine whether the input (or its first letter) matches any of them.

If you want to make it easier to change output formatting, I would even recommend that you go as far as using a JSON file instead of a text one.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.