Saturday, 31 August 2013

Trying to make a periodic table

Trying to make a periodic table

I have a file called "periodic_table". Inside this file is multiple lines.
Each line has an atomic number on the side and a corresponding element
name on the right like this:
1 Hydrogen
2 Helium
3 Lithium
4 Beryllium
5 Boron
6 Carbon
7 Nitrogen
8 Oxygen
9 Fluorine
10 Neon
11 Sodium
12 Magnesium
13 Aluminium
14 Silicon
etc...
I made a program that asks for either the element name or number, and
prints out the corresponding value in the dictionary. If the user inputs 1
it will print Hydrogen, similarly if the user inputs Silicon it will
output 14. HOWEVER - I want the program to inform the user if he enters a
non existent atomic number (such as 150) or a non existent element (such
as Blanket or any other string). Tried using an if but it printed out an
infinite loop.
element_list = {}
name = input("Enter element number or element name: ")
while name:
with open("periodic_table.txt") as f:
for line in f:
(key, val) = line.split()
element_list[int(key)] = val
if name == key:
print(val)
elif name == val:
print(key)
name = input("Enter element number or element name: ")
Thanks in advance fellas!

No comments:

Post a Comment