dict problem

A

Alistair King

Jon said:
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.

a


--
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
 
J

Jon Clements

Alistair said:
Jon said:
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.
 
B

Ben Finney

Jon Clements said:
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..."

Even better, work on a minimal program to do nothing but reproduce the
unexpected behaviour. If you get to such a program and still don't
understand, then post it here so others can run it themselves and
explain.
 
A

Alistair King

Ben said:
Even better, work on a minimal program to do nothing but reproduce the
unexpected behaviour. If you get to such a program and still don't
understand, then post it here so others can run it themselves and
explain.
ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.

--
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
 
B

Ben Finney

Alistair King said:
ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.

Can you please post a small, complete program that shows the behaviour
you want explained?

You get this program by writing it -- either by cutting away
irrelevant parts of the existing program, or (better) writing a new
program from scratch that does nothing except demonstrate the
behaviour.

Note that in the process of getting such a program behaving this way,
you may end up understanding the problem better.
 
A

Alistair King

Ben said:
Can you please post a small, complete program that shows the behaviour
you want explained?

You get this program by writing it -- either by cutting away
irrelevant parts of the existing program, or (better) writing a new
program from scratch that does nothing except demonstrate the
behaviour.

Note that in the process of getting such a program behaving this way,
you may end up understanding the problem better.
i have seemed to work out most of the problems from the previous code,
now i have:
....................................................................................................................................................

heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen, eg,
'C', 'N', 'S', 'Br'...: ")

print DSvalues

def updateDS1v(Fxas, x):
if Fxas != 0:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms

updateDS1v(FCas, 'C')

print DS1v
....................................................................................................................................................

the problem now is converting badly formatted dictionary values into floats
ive been trying something like this, where 'value' is a typical entry
into the dictionary:
....................................................................................................................................................

IDLE 1.1
and get the error, in IDLE and the code as:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
print float(value)
ValueError: invalid literal for float(): '0.064250000000001084'

....................................................................................................................................................

Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

I know it would be easier to create a properly formatted dictionary
again and i will do that but it would be good to know as i want to get
this program running properly to get some results i need for work.
Everything else seems to be working fine but this.

thanks

a

--
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
 
F

Fredrik Lundh

Alistair said:
Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

help(str) describes what you can do with a string (an object of type
'str', that is). among the methods listed, you'll find:
| strip(...)
| S.strip([chars]) -> string or unicode
|
| Return a copy of the string S with leading and trailing
| whitespace removed.
| If chars is given and not None, remove characters in chars instead.
| If chars is unicode, S will be converted to unicode before stripping

which looks like it should be pretty useful for this specific case:
0.064250000000001084

</F>
 
A

Alistair King

Fredrik said:
Alistair King wrote:

Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

help(str) describes what you can do with a string (an object of type
'str', that is). among the methods listed, you'll find:

| strip(...)
| S.strip([chars]) -> string or unicode
|
| Return a copy of the string S with leading and trailing
| whitespace removed.
| If chars is given and not None, remove characters in chars instead.
| If chars is unicode, S will be converted to unicode before stripping

which looks like it should be pretty useful for this specific case:
0.064250000000001084

</F>
Thanks..

the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
...............................................................................................................................
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

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

...............................................................................................................................
it works perfectly now

thanks for the help

a

--
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
 
P

Peter Otten

Alistair said:
the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')
it works perfectly now

Probably not. Have you manually verified the result with more than one
example? Where does 'heavy' come from? Is that black hole 'atoms'
intentional?

# I'm just guessing here
for k, v in DSvalues.iteritems():
DSvalues[k] = float(v)

def updateDS1v(Fxas, x):
DS1v[x] = DS1v.get(x, 0) + Fxas*DSvalues[x]

Peter
 
A

Alistair King

Peter said:
Alistair King wrote:

the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:


heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

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


it works perfectly now

Probably not. Have you manually verified the result with more than one
example? Where does 'heavy' come from? Is that black hole 'atoms'
intentional?

# I'm just guessing here
for k, v in DSvalues.iteritems():
DSvalues[k] = float(v)

def updateDS1v(Fxas, x):
DS1v[x] = DS1v.get(x, 0) + Fxas*DSvalues[x]

Peter
yea...sorry i snipped one line by accident for the email
should be:

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

thanks

a

--
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
 
B

Ben Finney

Alistair King said:
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen, eg,
'C', 'N', 'S', 'Br'...: ")

print DSvalues

def updateDS1v(Fxas, x):
if Fxas != 0:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms

updateDS1v(FCas, 'C')

print DS1v

I see from later posts that you have found solutions to your current
issues.

Just a note on what you posted: Please make sure that you post your
message as plain text. As you can see from what I quoted above, the
indentation is completely lost in the code you posted.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top