#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    15

    Strange thing happening with for loop!


    Hello,

    I have the following simple for loop that goes through each line in a list and if it has '>' in it prints it and otherwise has to compare each character of the line with a HXB2 sequence and print the differences.

    Code:
    linenumber=0
    
    for line in list:
        try:
            if '>' in line:
                print string.strip(list[linenumber])
            else: 
                sequence=line
                pos=0
                for char in sequence:
                    if char != HXB2[pos]:
                        print char,pos+1,
                    pos+=1
                print "This line is done"
        except:
            pass
        linenumber=linenumber+1
    Strangely however, in the else statement after the for loop is completed it doesn't continue to print the "This line is done" statement and it goes to the outer for loop and reads the next line. Only when all the lines are read, it prints the "This line is done" statement. Any ideas why that might be happening?

    Thanks for your help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    I'm not sure why you have this problem, because your script works for me. Here is a slightly shorter version:
    Python Code:
    for line in lst:
        if ">" in line:
            print line
        else:
            for pos, char in enumerate(line):
                if char != HXB2[pos]:
                    print char, pos+1,
            print "This line is done"


    Regards, mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by sam_kh918
    Hello,
    Code:
    linenumber=0
    
    for line in list:
        try:
            if '>' in line:
                print string.strip(list[linenumber])
            else: 
                sequence=line
                pos=0
                for char in sequence:
                    if char != HXB2[pos]:
                        print char,pos+1,
                    pos+=1
                print "This line is done"
        except:
            pass
        linenumber=linenumber+1
    Perhaps pos is going past the end of HXB2 before char gets to the end of sequence. Then "if char != HXB2[pos]" would raise an exception, and jump to the except clause, skipping the print entirely.

    Code:
    print string.strip(list[linenumber])
    Also, don't rely on the string module, it's deprecated. Instead call strip() as a method of the string itself, e.g.: "whatever".strip()
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    15
    Excellent! I used both your feedbacks and it solved the problem. I just changed "sequence=line" to "sequence=line.strip()", as you said the pos was going pass the HXB2 before char gets to the end of the sequence. Also I didn't know you could use strip() without the string prefix. Thanks for that.

IMN logo majestic logo threadwatch logo seochat tools logo