Feb-12-2018, 05:08 PM
(Feb-12-2018, 04:29 PM)BigEasy Wrote:for x in s1: for y in s2: if y != x: count += 1 #return count if count < 2: return 1 else: return 2
You are looping through each character in both strings, but you're probably not doing what you expect. You compare the first character of the first string against every character in the second string. And then you return, without ever getting to the second character of the first string.
As a demonstration, here's what you're doing, with print functions added:
>>> first = 'spam' >>> second = 'eggs' >>> for char in first: ... for compare in second: ... print("{0} == {1} => {2}".format(char, compare, char==compare)) ... break ... s == e => False s == g => False s == g => False s == s => TrueWhat you probably want to do, is use indexing instead of looping over the string itself. That way, you're comparing characters at the same positions in each string. Something like:
>>> first = 'spam' >>> second = 'eggs' >>> for ndx, ch in enumerate(first): ... print("{0} == {1} => {2}".format(ch, second[ndx], ch==second[ndx])) ... s == e => False p == g => False a == g => False m == s => False