Alistair King wrote: > Jon Clements wrote: > > > > 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? > >> >> > >> >> > >> > > > > > > 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. > > > > > > > > > sorry, > > this has been a little rushed > > XDS is defined before the function and is a float. > the Fxas values are also and they are integers > > > now ive tried > > 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 > > from this i get the error: > > Traceback (most recent call last): > File "DS1excessH2O.py", line 242, in ? > updateDS1v(FCas, 'C', XDS) > File "DS1excessH2O.py", line 239, in updateDS1v > atoms = DS1v.get(x) + Fxas*XDS > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > > with single quotes (FCas, 'C', XDS) to retrieve the value for that key > from the dictionary and then create the new value and replace the old value.
One of Fxas or XDS is a string then... Again, no-one can help you if we can't see what's actually there. What are FCas, FHas etc... do they relate to the element? If so, isn't that a dictionary in itself? And your update function is still an infinite loop! We're still in the dark as to what you're trying to do, try describing something like: "for each element there is an associated 'F' value. For each element in an existing molecule I wish to change the number of 'whatever' to be 'whatever' + my 'F' value * value XDS..." Jon. -- http://mail.python.org/mailman/listinfo/python-list