Nested Loops Wont Loop

Hello, I’m looking for some help in this loop.

def common_letters(string_one, string_two):

for a in string_one:

if a in string_two:

    common_characters.append(a)

return common_characters

print(common_letters(“allison”, “allison”))

This is my code.

My output is [ ‘a’ ] .

What I was expecting was that the loop would go through each character of string_one, ‘allison’, evaluate that each character is in string_two, also ‘allison’, and then add each character to the empty list common_characters.

It seems to work for the first letter and then stops. Please show me the error of my ways!

Be sure to declare that at the top of the function body so it is available for appending.

Also, be sure your indentation is correct else Python might not run the code you expect.

1 Like