0

I'm looping through a set of string and chars. I want to find the strings which contain the specified chars and then add the number of this string to see if the sum of the numbers from one char are bigger than the other.

Problem is that in line 21:

if char_1 and char_2 in dat:

after the forth time it loops it find the char 'A' in the forth line of the sample: 210 B C D

But there is no 'A' in this line.

Can somebody tell me thy it finds this char in this string?

Here is my complete code:

f = open('sample2.txt', 'r')
file = f.read()

data = []
chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
in_de = []

for line in file.split('\n'):
    data.append(line)

char_count = 0
index = 0
count = 0

while count < len(data):
    count_1 = 0
    count_2 = 0
    for dat in data:
        char_1 = chars[char_count]
        char_2 = chars[char_count+1]
        if char_1 and char_2 in dat:
            count_1 += int(dat[0:3])
            count_2 += int(dat[0:3])
        elif chars[char_count] in dat:
            count_1 += int(dat[0:3])
        elif chars[char_count+1] in dat:
            count_2 += int(dat[0:3])
    if count_1 > count_2:
        in_de.append('increase')
    else:
        in_de.append('decrease')
    char_count += 1
    count += 1

sum = 0

for line in in_de:
    if line == 'increase':
        sum += 1

print(sum)

Here is the sample file:

199  A
200  A B
208  A B C
210    B C D
200  E   C D
207  E F   D
240  E F G
269    F G H
260      G H
263        H
PoPhoRmA
  • 23
  • 5
  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – tobias_k Sep 20 '22 at 13:47
  • The dupe is about `or`, but it's the same for `and`. The condition is not evaluated as `if (char_1 in dat) and (char_2 in dat):` but `if bool(char_1) and (char_2 in dat):` – tobias_k Sep 20 '22 at 13:48
  • @tobias_k I tried your suggestion, seems like it should solve the problem but still find the char 'A' in String '210. B C D'. – PoPhoRmA Sep 20 '22 at 13:55

0 Answers0