Lisa Ka
Lisa Ka

Reputation: 13

No errors, just doesn't print or do anything

I am pretty a beginner and I'm looking for help. I am supposed to write a simple programm which reads numbers from a file (they are ordered in two columns like this:

3 788506
255 879405
3 687899
255 697879 etc)

and always pairwise subtracts the number near 255 from the number near 3. The differences should be appended to a list. I also have to check whether the pair is rigt (e.g. that it's always 3 and 255 one after the other and not two 255s). So far I think I'm ready, but it doesn't do anything. I spent hours looking for my mistake, but I just cannot see what went wrong. I would appreciate any help.

filepath = "C:/liz/RT1-1.dat"
f = open (filepath, 'rU')
reac3 = []
reac255 = []
right_list = []
wrong_list = []
very_wrong_list =[]
li = [i.strip().split() for i in f.readlines()]
for element in li:
    if int(element[0]) == 3: reac3.append(element[-1])
    elif int(element[0]) == 255: reac255.append(element[-1])
k = 0
for i in range (0, len(li)+1, 2): #0,2,4,6,8 etc
    if li[i][0] == 3 and li[i+1][0] == 255:
            difference = int(reac255[k]) - int(reac3[k])
            print int(difference)
            k+=1
            if difference > 300 and difference < 1200: right_list.append(difference)
            else: wrong_list.append(difference)
    else: very_wrong_list.append(li[i])
print right_list

Upvotes: 0

Views: 147

Answers (1)

Mahmoud Aladdin
Mahmoud Aladdin

Reputation: 546

i.strip().split() will return 2 strings .. therefore your comparison li[i][0] == 3 & li[i+1][0] == 5 should fail as li[i][0] & li[i+1][0] are still strings.

Also notice, that since len(li) should be even, then xrange(0, len(li) + 1, 2) will eventually make i = len(li) which should be out of the list boundaries.

Upvotes: 2

Related Questions