[Tutor] if >= 0
Alan Gauld
alan.gauld at btinternet.com
Mon Feb 10 17:25:48 CET 2014
On 10/02/14 15:32, rahmad akbar wrote:
> hey again guys, i am trying to understand these statements,
>
> if i do it like this, it will only print the 'print locus' part
>
> 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.
> elif element.find('ACCESSION'):
> accession += element
> elif element.find('ORGANISM'):
> organism += element
>
> print locus
> print accession
> print organism
I assume by not printing you mean the other two print statements
don't print anything visible? Or are you actually getting an
error message?
Without seeing how accession and organism are defined its hard
to know why you are not seeing anything. The print statements
are not inside the loop so should always (all three) print
something,even if the something is an empty string.
> once i add >= 0 to each if conditions like so, the entire loop works and
> prints the 3 items
> for element in in_file:
> if element.find('LOCUS') >= 0:
> locus += element
This should always be executed unless LOCUS happens to be at the start
of the line *or missing*. The greater than zero test eliminates the
casers where LOCUS does not exist, in these cases it will move onto the
next elif clause. Presumably thats why you now get 3 values printed.
But that assumes you initialised the variables to empty strings at the
start?
> elif element.find('ACCESSION') >= 0:
> accession += element
> elif element.find('ORGANISM') >= 0:
> organism += element
>
> print locus
> print accession
> print organism
>
> why without '>= 0' the loop doesnt works?
Can you show us some output? I don't really understand what
you mean by "doesn't work". Do you get any error messages?
Or is it just the lack of print outputs (that really have
nothing to do with the loop part)?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list