For loop appending wrong Value

Hey guys I'm missing something here I want to append 'a' to my answers if the two values match and 'b' if they don't but it always appends 'b'

so the number before the word should be equal to the length of the word

import re

def is_a_valid_message(message):
answers = []
x = re.findall(r"([0-9]+)([a-zA-Z]+)", message)    
for k  in x:
    y = len(k[1])
    z = k[0]
    if y == z:
        answers.append('a')
        print(k[0], k[1])   
    else:
        answers.append('b')
        print(k[0], len(k[1]))   
print(answers)

The first test string is "3hey5hello2hi"

which outputs

3 3
5 5
2 2
['b', 'b', 'b']

I



Read more here: https://stackoverflow.com/questions/65708561/for-loop-appending-wrong-value

Content Attribution

This content was originally published by widgey at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: