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