Nothing appears when you run your program because it runs in an infinite loop.
The while (count<lenth): statement tells python to keep running while count is less than "lenth", but since the value of count never changes, this is always true, and python continues running endlessly.
It's rare that you actually need indexes or counters like this in python. You fix it by getting rid of the index by using a for loop, like so:
def similarStrings(str1, str2):
length = len(str1)
i = 0
for char1, char2 in zip(str1, str2):
if char1 == char2:
i = i+1
if i >= length-1:
return True
else:
return False
I'd rewrite the function like so though:
def similarStrings(str1, str2, difference=1):
from itertools import izip_longest
return sum(
char1 != char2
for char1, char2 in izip_longest(str1, str2)
) <= difference
I wrote a test suite, of sorts:
def test(s1, s2):
if similarStrings(s1, s2):
print 'SIMILAR:'
else:
print 'NOT SIMILAR:'
print ' ', s1
print ' ', s2
def main():
test('', '')
test('abc', 'abc')
test('abcd', 'abc')
test('abc', 'abcd')
test('abcd', 'abce')
test('abcd', 'abcee')
test('abcdd', 'abce')
test('abcdd', 'abcee')
if __name__ == '__main__':
exit(main())
Which gave me these results, using my final implementation:
SIMILAR:
SIMILAR:
abc
abc
SIMILAR:
abcd
abc
SIMILAR:
abc
abcd
SIMILAR:
abcd
abce
NOT SIMILAR:
abcd
abcee
NOT SIMILAR:
abcdd
abce
NOT SIMILAR:
abcdd
abcee
similarStringsfunction? Are you calling it? What are you doing with the output?countso it is an infinite loop withwhile