On 3/10/06, sjw28 <[EMAIL PROTECTED]> wrote:

Basically, I have a code with is almost finished but I've having difficultly
with the last stage of the process. I have a program that gets assigns
different words with a different value via looking them up in a dictionary:

eg if THE is in the writing, it assigns 0.965

and once the whole passage is read it returns all the numbers in the format
as follows:

['0.965', '1.000', '0.291', '1.000', '0.503']

However, I can't seem to get the program to treat the numbers as numbers. If
I put them in the dictionary as 'THE' = int(0.965) the program returns 1.0
and if I put 'THE' = float(0.965) it returns 0.96555555549 or something
similar. Neither of these are right! I basically need to access each item in
the string as a number, because for my last function I want to multiply them
all together by each other.

I have tried two bits of code for this last bit, but neither are working
(I'm not sure about the first one but the second one should work I think if
I could figure out how to return the values as numbers):

1st code
value = codons[x] * codons[x+1]
x = (int)
x = 0

print value

x +=2

if (x<r):
    new_value = value * codons[x]
    value = new_value
    x +=1
else:
    print new_value

This gives the error message
Traceback (most recent call last):
  File "C:\Python24\code2", line 88, in -toplevel-
    value = codons[x] * codons[x+1]
NameError: name 'x' is not defined

Code 2 - the most likely code
prod = 1
for item in (codons): prod *= item
prod
print prod

Gives this error message:
Traceback (most recent call last):
  File "C:\Python24\code2", line 90, in -toplevel-
    for item in (codons): prod *= item
TypeError: can't multiply sequence by non-int


Can anyone help me solve this problem?
Thanks.


This is exactly what the decimal module was created for.

It will take those strings, let you do your computations and give you back the exact decimal values with exactly the precision you want. You get to decide what type of rounding rules you want.

If you don't have it already (say you're using 2.3 for example) you can get it from http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html. Otherwise, just import it and run help(decimal) to learn it.

I'd show you but I just discovered I'm running 2.3 on this Mac so I need to upgrade.

If you have trouble using decimal, let us know.

Anna

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to