For loop issues - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: For loop issues (/thread-8269.html) |
For loop issues - BigEasy - Feb-12-2018 What am I doing wrong here? From what I can tell, my for loop (for x in s1) is not actually looping through the characters. But I don't know how to fix it. # Type your code here def find_mismatch(s1,s2): count = 0 s1 = s1.lower() s2 = s2.lower() s1 = list(s1) s2 = list(s2) #print(s1, s2) if len(s1) != len(s2): return 2 elif s1 == s2: return 0 else: for x in s1: for y in s2: if y != x: count += 1 #return count if count < 2: return 1 else: return 2
RE: For loop issues - nilamo - Feb-12-2018 (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 RE: For loop issues - BigEasy - Feb-12-2018 Thank you for the response! I understand the enumerate method, but don't understand the code right below that line. Would you be able to explain in simpler terms? Thank you!! RE: For loop issues - nilamo - Feb-12-2018 Alright, let's go without enumerate, then. What we really want, is an index. That way, we can compare characters from both strings, that are at the same position in the string. The first character of each string, then the second character of each, then the third, etc. Does this make more sense? >>> first = 'socks' >>> second= 'laces' >>> for ndx in range(len(first)): ... left = first[ndx] ... right = second[ndx] ... print("Index we're comparing: {0}".format(ndx)) ... print("Character of first string at that index: {0}".format(left)) ... print("Character of second string at that index: {0}".format(right)) ... print("Are they the same? {0}".format(left == right)) ... print(" -- ") ... Index we're comparing: 0 Character of first string at that index: s Character of second string at that index: l Are they the same? False -- Index we're comparing: 1 Character of first string at that index: o Character of second string at that index: a Are they the same? False -- Index we're comparing: 2 Character of first string at that index: c Character of second string at that index: c Are they the same? True -- Index we're comparing: 3 Character of first string at that index: k Character of second string at that index: e Are they the same? False -- Index we're comparing: 4 Character of first string at that index: s Character of second string at that index: s Are they the same? True -- |