variable creation

A

Alistair King

Hei all,

im trying to create a list of variables for further use:


Els = raw_input("Are there any further elements you would like to
include? if so type the element, eg, Pd. If not type No: ")

if Els != 'No':
el = Els in pt
while el == 1 and Els != 'Next':
elements = []
elements.insert(-1, Els)

Els = raw_input("Which further element would you like to
include, eg, Pd, if not type Next: ")
el = Els in pt

while el == 0 and Els != 'Next':
Els = raw_input("This is not an element in the periodic
table, please try again, eg Pd, If you dont wish to include any more,
type Next: ")
if el == 1:
elements.insert(-1, Els)

while el == 0:
Els = raw_input("This is not an element in the periodic
table, please try again, eg Pd. If you dont wish to include any more,
type Next: ")

print elements


this works to a certain extent but gets stuck on some loop. Im a
beginner and am not sure where im going wrong.

here is a portion of the dictionary containing typical elements:

pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B':
10.811}


Ali


--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366
 
J

Justin Azoff

Alistair said:
Hei all,

im trying to create a list of variables for further use: [snip]
this works to a certain extent but gets stuck on some loop. Im a
beginner and am not sure where im going wrong.

You are trying to do too much in one function. Split those loops up
into a few little ones and the program will work... or if it doesn't,
you'll know exactly where the problem is.

def get_element(pt):
"Return None or a single element from the periodic table"
while 1:
el = raw_input("Which element would you like to include? ")
if not el: #blank answer
return
if el in pt:
return pt[el]
print "This element is not in the periodic table, please try
again"

def get_elements(pt):
elements = []
while 1:
el = get_element(pt)
if not el:
break
elements.append(el)

return elements

See how using two separate functions makes it easy to test?
In [10]:print get_element(pt)
Which element would you like to include? X
This element is not in the periodic table, please try again
Which element would you like to include? H
1.0079400000000001

In [11]:print get_elements(pt)
Which element would you like to include? Z
This element is not in the periodic table, please try again
Which element would you like to include? Li
Which element would you like to include? B
Which element would you like to include? H
Which element would you like to include?
[6.9409999999999998, 10.811, 1.0079400000000001]


Now, since the information for a single element consists of more than
just a single number, you'll probably want to make a class for them.
Once you have an object for every element, you can add them to a class
for the periodic table.
 
J

Jon

Hi,
I'm not sure if this is exactly what you had in mind but what about
something like this:

elements = []
Els = ""
pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182,
'B':10.811}
while Els != 'No':
Els = raw_input("""Are there any further elements you would like to
include? if so type the element, eg, Pd. If not type No:""")
if pt.has_key(Els):
elements.append({Els:pt[Els]})

print elements
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top