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

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Sun Jun 1 00:31:25 CEST 2014


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


More information about the Tutor mailing list