| From: sjw28 
| 
| 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
| 
You defined x after you tried to use it and that was the error message: x was 
not defined yet in the first line of your code.  Also, what were you trying to 
do with x = (int)?

[cut]
| 
| Code 2 - the most likely code
| prod = 1
| for item in (codons): prod *= item
| prod

This line is not necessary. In the interactive window typing a variable name 
will cause the value to be shown to you. But in a script it does nothing. The 
next line (print prod) is the way to see the product.

Also, what is item in Code 2 above: something like 'THE' or something like 
'0.965'? If you did something like putting the values in a dictionary (named 
something like 'values') then you should have something like

    prod *= float(values[item])

instead of what you have above.

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

Others have mentioned why 0.965 doesn't look exactly like that when you view it 
in repr() form and they have told you where you can read more about this and 
what other module you might use to avoid this problem.

HOWEVER, I don't think you need the decimal module for what you are doing. You 
have 17 digits of precision in the floating point numbers you are using. The 
numerical value that python is using for 0.965 is only 0.00000000000000003 
units off from the true value. When you get done calculating your product I am 
guessing that you don't need 17 digits of precision (since your codon values 
only had 3 digits of precision). If you want a nice representation of your 
final answer showing maybe 4 of the leading digits, consider using the fpformat 
option, sci:

>>> ll=['0.965', '1.000', '0.291', '1.000', '0.503']
>>> p=1
>>> for li in ll:
...  p*=float(li)
...  
>>> print p
0.141249945
>>> from fpformat import sci
>>> sci(0.141249945,3) #rounds to 3rd digit after the first non-zero one
'1.412e-001'
>>> float(_) #makes it a floating point number (a little ugly)
0.14119999999999999
>>> str(_) #rounds it to 12 digits of precision (or 1 part per trillion)
'0.1412'
>>> print str(float(sci(0.141249945,3))) #putting it all together
0.1412

Chris


What are you going to do with your product when you are done computing it?
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to