<br><br><div><span class="gmail_quote">On 3/10/06, <b class="gmail_sendername">sjw28</b> &lt;<a href="mailto:brainy_muppet@hotmail.com">brainy_muppet@hotmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Basically, I have a code with is almost finished but I've having difficultly<br>with the last stage of the process. I have a program that gets assigns<br>different words with a different value via looking them up in a dictionary:
<br><br>eg if THE is in the writing, it assigns 0.965<br><br> and once the whole passage is read it returns all the numbers in the format<br>as follows:<br><br>['0.965', '1.000', '0.291', '1.000', '0.503']<br><br>However, I can't seem to get the program to treat the numbers as numbers. If
<br>I put them in the dictionary as 'THE' = int(0.965) the program returns 1.0<br>and if I put 'THE' = float(0.965) it returns 0.96555555549 or something<br>similar. Neither of these are right! I basically need to access each item in
<br>the string as a number, because for my last function I want to multiply them<br>all together by each other.<br><br>I have tried two bits of code for this last bit, but neither are working<br>(I'm not sure about the first one but the second one should work I think if
<br>I could figure out how to return the values as numbers):<br><br>1st code<br>value = codons[x] * codons[x+1]<br>x = (int)<br>x = 0<br><br>print value<br><br>x +=2<br><br>if (x&lt;r):<br>&nbsp;&nbsp;&nbsp;&nbsp;new_value = value * codons[x]
<br>&nbsp;&nbsp;&nbsp;&nbsp;value = new_value<br>&nbsp;&nbsp;&nbsp;&nbsp;x +=1<br>else:<br>&nbsp;&nbsp;&nbsp;&nbsp;print new_value<br><br>This gives the error message<br>Traceback (most recent call last):<br>&nbsp;&nbsp;File &quot;C:\Python24\code2&quot;, line 88, in -toplevel-<br>&nbsp;&nbsp;&nbsp;&nbsp;value = codons[x] * codons[x+1]
<br>NameError: name 'x' is not defined<br><br>Code 2 - the most likely code<br>prod = 1<br>for item in (codons): prod *= item<br>prod<br>print prod<br><br>Gives this error message:<br>Traceback (most recent call last):<br>
&nbsp;&nbsp;File &quot;C:\Python24\code2&quot;, line 90, in -toplevel-<br>&nbsp;&nbsp;&nbsp;&nbsp;for item in (codons): prod *= item<br>TypeError: can't multiply sequence by non-int<br><br><br>Can anyone help me solve this problem?<br>Thanks.</blockquote>
<div><br>
<br>
This is exactly what the decimal module was created for. <br>
<br>
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.<br>
<br>
If you don't have it already (say you're using 2.3 for example) you can
get it from
<a href="http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html">http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html</a>.
Otherwise, just import it and run help(decimal) to learn it. <br>
<br>
I'd show you but I just discovered I'm running 2.3 on this Mac so I need to upgrade. <br>
<br>
If you have trouble using decimal, let us know. <br>
<br>
Anna</div></div><br>