dict problem

Peter Otten __peter__ at web.de
Wed Oct 25 05:13:05 EDT 2006


Alistair King wrote:

> 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



More information about the Python-list mailing list