[Tutor] Break outside the loop error (line 23)

2014-05-31 Thread Sasi -
Hi, i tried to make a code to execute the functions that i described below. 
However i got a break outside the loop error and i have tried to tab and all 
that but i still get the same error for line 23. I'm using python 2.7
Please help me. I'm completely new to coding (just started last week)Thank you 
for your time, i have pasted the code below.
## open both filesfile1 = open('human_brain_proteins.csv')file2 = 
open('human_plasma_proteins.csv')#file1 = open('log1')#file2 = open('log2')
## createdd a counter to count linescount1 = 0count2 = 0
## define 3 lists to be filled in latercommon = list()brain = list()plasma = 
list()
## Created the lists for brain and plasma before searching for common 
bioseq.while True:count1 += 1s1=file1.readline().partition(',')[0]if s1 
and count1  1:brain.append(s1)if not s1:
breakwhile True:count2 += 1s2=file2.readline().partition(',')[0]if s2 
and count2  1:plasma.append(s2)if not s2:break
## Compared the lists to find out common bioseq., add the common bioseq. into 
the common list,## then remove the common bioseq. from both listsfor item1 in 
brain:for item2 in plasma:if item1 == item2:
common.append(item1)brain.remove(item1)
plasma.remove(item2)
## closed both filesfile1.close()file2.close()
## print out the listsprint Common biosequence:print common,\nprint Brain 
specific biosequence:print brain,\nprint Plasma specific biosequence:print 
plasma,\n




  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Break outside the loop error (line 23)

2014-05-31 Thread Wolfgang Maier

On 31.05.2014 11:23, Sasi - wrote:

Hi, i tried to make a code to execute the functions that i described
below. However i got a break outside the loop error and i have tried to
tab and all that but i still get the same error for line 23. I'm using
python 2.7


Hi,
as a general rule, copy paste the traceback of the actual python 
exception instead of summarizing it.

In this specific case the error is obvious though (see below).


Please help me. I'm completely new to coding (just started last week)
Thank you for your time, i have pasted the code below.

## open both files
file1 = open('human_brain_proteins.csv')
file2 = open('human_plasma_proteins.csv')
#file1 = open('log1')
#file2 = open('log2')

## createdd a counter to count lines
count1 = 0
count2 = 0

## define 3 lists to be filled in later
common = list()
brain = list()
plasma = list()

## Created the lists for brain and plasma before searching for common
bioseq.
while True:
 count1 += 1
 s1=file1.readline().partition(',')[0]


your while loop ends here because the next line is not indented 
(probably not what you intended).



if s1 and count1  1:
 brain.append(s1)
 if not s1:
 break


since you are not inside the while loop anymore, there is nothing to 
break from.



while True:
 count2 += 1
 s2=file2.readline().partition(',')[0]
if s2 and count2  1:
 plasma.append(s2)
 if not s2:
 break

## Compared the lists to find out common bioseq., add the common bioseq.
into the common list,
## then remove the common bioseq. from both lists
for item1 in brain:
 for item2 in plasma:
 if item1 == item2:
 common.append(item1)
 brain.remove(item1)
 plasma.remove(item2)

## closed both files
file1.close()
file2.close()

## print out the lists
print Common biosequence:
print common,\n
print Brain specific biosequence:
print brain,\n
print Plasma specific biosequence:
print plasma,\n



additional suggestion:
read about the set datatype of python and its intersection and 
difference methods 
(https://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset). 
It looks like handling your data as two sets instead of two lists should 
be much more convenient.


Best,
Wolfgang
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Break outside the loop error (line 23)

2014-05-31 Thread Alan Gauld

On 31/05/14 10:23, Sasi - wrote:

Hi, i tried to make a code to execute the functions that i described
below. However i got a break outside the loop error


Always post the full error message it usually contains
lots of useful detail.


## Created the lists for brain and plasma before searching for common
bioseq.
while True:
 count1 += 1
 s1=file1.readline().partition(',')[0]
if s1 and count1  1:


This line is not indented so the loop stops on
the previous line.



 brain.append(s1)
 if not s1:
 break


But your 'break' is here so it is outside the loop.

You need to indent the whole section from

if s1 and count1  1:

so that it is inside the loop.



while True:
 count2 += 1
 s2=file2.readline().partition(',')[0]
if s2 and count2  1:
 plasma.append(s2)
 if not s2:
 break


And the same problem here.
The indentation defines what is inside the loop.

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor