dict problem

A

Alistair King

Hi,

ive been trying to update a dictionary containing a molecular formula, but seem to be getting this error:


Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}



#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and am lost?


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 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366
 
R

Roberto Bonvallet

Alistair said:
Hi,

ive been trying to update a dictionary containing a molecular formula, but seem to be getting this error:


Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}

Try DS1v['C'] instead of DS1v[C].

Cheers,
 
R

Roberto Bonvallet

Roberto said:
Alistair said:
DS1v = {'C': 6, 'H': 10, 'O': 5}

Try DS1v['C'] instead of DS1v[C].
updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

Sorry, I hadn't seen this part.
Change C, H, O, ... with 'C', 'H', 'O', ...
 
P

Peter Otten

Alistair said:
Hi,

ive been trying to update a dictionary containing a molecular formula, but
seem to be getting this error:


Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}



#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and
am lost?

Ali,

the NameError stems from the variable C not being defined.
This can be fixed either by defining it:

C = "C"
updateDS1v(FCas, C, XDS)

or by calling updateDS1v() with a string constant.

updateDS1v(FCas, "C", XDS)

However, there are so many errors in the updateDS1v() function that I
recommend reading an introductory text on Python before you proceed.

Python is so much /more/ fun if you have at least some clue :)

Peter
 
J

Jon Clements

Alistair said:
Hi,

ive been trying to update a dictionary containing a molecular formula, but seem to be getting this error:


Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}



#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and am lost?

I strongly suggest reading through the tutorial.

I don't think there's enough code here for anyone to check it properly.
For instance, it looks like FCas exists somewhere as it's barfing on
trying to find C. Where is XDS defined etc...?

I can't see updateDS1v() ever completing: any Fxas passed in not equal
to 0 will repeat indefinately.

I'm guessing unless C is meant to be a variable, you mean to pass in
the string 'C'.

A dictionary already has it's own update method....

Perhaps if you explain what you're trying to do in plain English, we
could give you some pointers.

Jon.
 

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,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top