Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop issues
#1
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
Output:
Your function return is NOT CORRECT. For example when your function is called as shown below: find_mismatch('Hello There', 'helloothere') Your function returns: 2 The correct return value is: 1
Reply
#2
(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 => True
What 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
Reply
#3
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!!
Reply
#4
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
 --
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 2,085 Aug-25-2018, 10:44 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020