[Tutor] if >= 0

ALAN GAULD alan.gauld at btinternet.com
Tue Feb 11 00:58:28 CET 2014


CCing Tutor list.
 
here are the entire code 
>
>
>import sys
>in_file = open(sys.argv[1], 'r').readlines()
>locus = ''
>accession = ''
>organism = ''
>
>OK, as suspected you initialise these to empty strings so they almost certainly 
are being printed they just don't contain anything. Its easilyy proved by making 
the initial value something visible like a dash '-'... say

for element in in_file:
>
>  if  element.find(LOCUS'):
>    locus += element
>
>
>The only time this is not executed is if LOCUS is at the
>
>very start of the line. In *every* other case this will
>be executed. Even if LOCUS is not in the element.
>
>
>
>why is that?Because the Python 'if' test treats any number other than zero as True.
And the string find() method returns the index at which the string is 
found or -1. So the only time you get zero(False) is if the string starts 
on the first character. If the string is missing you get a -1 which is 
considered True

When you added the >=0 you eliminated the cases where find() 
returned -1, ie where the string was not found and so you checked 
the other search cases.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140210/4c3e2906/attachment-0001.html>


More information about the Tutor mailing list