Thursday, 29 August 2013

How to call an item from a dictionary in python?

How to call an item from a dictionary in python?

So I have a dictionary which consists of a number and its definition is
the corresponding element on the periodic table.
I have built up the dictionary like this:
for line in open("periodic_table.txt"):
temp.append(line.rstrip())
for t in temp:
if t[1] != " ":
elements[x] = t[3:]
else:
elements[x] = t[2:]
x += 1
I know that every element is in table because I ask the program to print
the dictionary. The point of this is that the user enters a number or
element name and the number and name are both printed like this:
while count == 0:
check = 0
line = input("Enter element number or element name: ")
if line == "":
count = count + 1
break
for key,value in elements.items():
if line == value:
print ("Element number for",line,"is",key)
check = 1
break
if line.isdigit():
if int(line) <= 118 and int(line) > 0:
print ("Element number",line, "is",elements[int(line)])
check = 1
break
if check == 0:
print ("That's not an element!")
This works for every element except the last one, Ununoctium, in the
dictionary. When the user enters 118 this element is printed, but if they
enter 'ununoctium' then the program says "That is not an element!". Why is
this and how do I fix it?
Thanks in advance

No comments:

Post a Comment