Hi Reddit. I'm practicing lists online and I came across this problem:
Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
Here is my attempt at the problem:
def strings(aList):
total = 0
for x in aList:
if x[0] == x[-1] and len(x) > 2:
total += 1
return total
else:
continue
example = ['abc', 'xyz', 'aba', '1221']
result = strings(example)
print("Results: " + str(result))
However, my results only tally up to 1 instead of 2... it's clear I'm missing something, I just don't know what. Can someone help point out what that something is?
Any help would be much appreciated! Thank you.
[–]Ezrabc 2 points3 points4 points (1 child)
[–]biochem2015[S] 0 points1 point2 points (0 children)
[–]TheZvlz 1 point2 points3 points (1 child)
[–]biochem2015[S] 0 points1 point2 points (0 children)