Python Forum
Python don't divide - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python don't divide (/thread-7085.html)



Python don't divide - SteLu - Dec-20-2017




Hello!!

I am coding what should be an easy program, to calculate few things. But, unfortunately, the things are not going as I planned.

This is my code:

[python]
seq_T4 ='MILKI...FEDVFGDFHEVTGL'

y = 0
w = 0
f = 0
num_aa = 0

for aa in seq_T4.lower():
num_aa += 1
if aa == 'y':
y += 1
elif aa == 'w':
w += 1
elif aa == 'f':
f += 1

aa_fluo = y + w + f
ratio = format((aa_fluo / num_aa),'.5f')

print ('the protein as', num_aa, 'aa, and therefore MW of roughly kDa', (num_aa*650)/1000)
print ('the number or Tyrosine is:', y)
print ('the number or Tryptophan is:', w)
print ('the number or Phenylalanine is:', f)
print ('the Fluorescent aa / total aa is % ', ratio)
[/pyhton]

output:

('the protein as', 487, 'aa, and therefore MW of roughly kDa', 316)
('the number or Tyrosine is:', 20)
('the number or Tryptophan is:', 5)
('the number or Phenylalanine is:', 18)
('the Fluorescent aa / total aa is % ', '0.00000')

The problem is that the ratio doesn't give the expected number. It look like Python does not know how to divide. I have tried to do the division in the shell and it work properly, but when I ask to do it in this code doesn't work.

I am running this version of Ipython :
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.

It is running on Ubuntu bash shell for windows 10

Thank you


RE: Python don't divide - j.crater - Dec-20-2017

Please edit your post so that Python code, errors and outputs are put within proper tags. And when copying code, use ctrl+shift+v to preserve indentation. You can find help here.


RE: Python don't divide - Afterdarkreader - Dec-20-2017

Print out the numerator and denominator and make sure that's correct. I almost guarantee that's your problem


RE: Python don't divide - SteLu - Dec-21-2017

Sorry, I am going to copu and paste the post with the tags

Hello!!

I am coding what should be an easy program, to calculate few things. But, unfortunately, the things are not going as I planned.

This is my code:

seq_T4 ='MILKILNEIASIGSTKQK....NTFEDVFGDFHEVTGL'

y = 0
w = 0
f = 0
num_aa = 0

for aa in seq_T4.lower():
    num_aa += 1
    if aa == 'y':
        y += 1
    elif aa == 'w':
        w += 1
    elif aa == 'f':
        f += 1

aa_fluo = y + w + f
ratio = aa_fluo / num_aa

print ('the protein as', num_aa, 'aa, and therefore MW of roughly kDa', (num_aa*650)/1000)
print ('the number or Tyrosine is:', y)
print ('the number or Tryptophan is:', w)
print ('the number or Phenylalanine is:', f)
a = 43/487
print ('the Fluorescent aa / total aa is ', format(ratio,'.5f'))
Output:
('the protein as', 38, 'aa, and therefore MW of roughly kDa', 24) ('the number or Tyrosine is:', 0) ('the number or Tryptophan is:', 0) ('the number or Phenylalanine is:', 3) ('the Fluorescent aa / total aa is ', '0.00000')

(Dec-20-2017, 09:02 PM)Afterdarkreader Wrote: Print out the numerator and denominator and make sure that's correct. I almost guarantee that's your problem

Hi Afterdarkreader,

I've already tried your suggestion and no luck at all. I've tried several solution, also
print(len(set_T4))
and even
print (43/487)
give as result 0.000000

Hello again,

I've used this version of python and Ipython and the script worked perfectly. any guess?

Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.


RE: Python don't divide - Larz60+ - Dec-21-2017

Added:
print(f'aa_fluo: {aa_fluo} num_aa: {num_aa}')
just before ratio calculation
I get (with that statement in place:
Output:
aa_fluo: 3 num_aa: 38 the protein as 38 aa, and therefore MW of roughly kDa 24.7 the number or Tyrosine is: 0 the number or Tryptophan is: 0 the number or Phenylalanine is: 3 the Fluorescent aa / total aa is  0.07895



RE: Python don't divide - SteLu - Dec-21-2017

Larz60+ thanks for your answer. Do you have any clue why it doesn't work with the previous code?


RE: Python don't divide - Larz60+ - Dec-21-2017

It worked for me, don't have a clue for yourself.
What you should always include is the verbatim error traceback, all of it as it contains
very valuable information.


RE: Python don't divide - SteLu - Dec-21-2017

I don't get any error, that's why I don't know what is wrong


RE: Python don't divide - casevh - Dec-21-2017

Python 2.x defaults to integer division.

Python 3.x uses floating point division.

Since Python 2.2, it has been possible to write code compatible with both Python 2 and 3 by using "from __future__ import division" to change the default behavior to use floating point division and using "//" to get integer division.

$ python
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/4
0


$ python3
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/4
0.75


$ python2
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> 3/4
0.75
>>> 3//4
0
>>>