Assertion error, eventhough my return value is the same
<pre><code>def interleave(s1,s2): #This function interleaves s1,s2 together
    guess = 0
    total = 0
    while (guess &lt; len(s1)) and (guess &lt; len(s2)):
        x = s1[guess]
        y = s2[guess]
        m = x + y
        print ((m),end ="")
        guess += 1
    if (len(s1) == len(s2)):
        return ("")
    elif(len(s1) &gt; len(s2)):
        return (s1[guess:])
    elif(len(s2) &gt; len(s1)):
        return (s2[guess:])

print (interleave("Smlksgeneg n a!", "a ie re gsadhm"))
</code></pre>

<p>For some reason, my test function gives an assertion error eventhough the output is the same as the code below.
Eg - "Smlksgeneg n a!", "a ie re gsadhm" returns "Sam likes green eggs and ham!"
but an assertion error still comes out</p>

<pre><code>def testInterleave():
    print("Testing interleave()...", end="")
    assert(interleave("abcdefg", "abcdefg")) == ("aabbccddeeffgg")
    assert(interleave("abcde", "abcdefgh") == "aabbccddeefgh")
    assert(interleave("abcdefgh","abcde") == "aabbccddeefgh")
    assert(interleave("Smlksgeneg n a!", "a ie re gsadhm") ==
                  "Sam likes green eggs and ham!")
    assert(interleave("","") == "")
    print("Passed!")

testInterleave()
</code></pre>

<p>The problem is that your function just prints the interleaved portion of the resulting string, it doesn't return it, it only returns the tail of the longer string.</p>

<p>Here's a repaired and simplified version of your code. You don't need to do those <code>if... elif</code> tests. Also, your code has a lot of superfluous parentheses (and one misplaced parenthesis), which I've removed.</p>

<pre><code>def interleave(s1, s2): 
    ''' Interleave strings s1 and s2 '''
    guess = 0
    result = ""
    while (guess &lt; len(s1)) and (guess &lt; len(s2)):
        x = s1[guess]
        y = s2[guess]
        result += x + y
        guess += 1
    return result + s1[guess:] + s2[guess:]   

def testInterleave():
    print("Testing interleave()...", end="")
    assert interleave("abcdefg", "abcdefg") == "aabbccddeeffgg"
    assert interleave("abcde", "abcdefgh") == "aabbccddeefgh"
    assert interleave("abcdefgh","abcde") == "aabbccddeefgh"
    assert (interleave("Smlksgeneg n a!", "a ie re gsadhm") 
        == "Sam likes green eggs and ham!")
    assert interleave("", "") == ""
    print("Passed!")

print(interleave("Smlksgeneg n a!", "a ie re gsadhm"))

testInterleave()
</code></pre>

<p><strong>output</strong></p>

<pre><code>Sam likes green eggs and ham!
Testing interleave()...Passed!
</code></pre>

<p>Here's a slightly improved version of <code>interleave</code>. It uses a list to store the result, rather than using repeated string concatenation. Using lists to build string like this is a common Python practice because it's more efficient than repeated string concatenation using <code>+</code> or <code>+=</code>; OTOH,<code>+</code> and <code>+=</code> on strings have been optimised so that they're fairly efficient for short strings (up to 1000 chars or so).</p>

<pre><code>def interleave(s1, s2): 
    result = []
    i = 0
    for i, t in enumerate(zip(s1, s2)):
        result.extend(t)
    i += 1
    result.extend(s1[i:] + s2[i:])
    return ''.join(result)
</code></pre>

<p>That <code>i = 0</code> is necessary in case either <code>s1</code> or <code>s2</code> are empty strings. When that happens the <code>for</code> loop isn't entered and so <code>i</code> won't get assigned a value.</p>

<p>Finally, here's a compact version using a list comprehension and the standard <code>itertools.zip_longest</code> function.</p>

<pre><code>def interleave(s1, s2):
    return ''.join([u+v for u,v in zip_longest(s1, s2, fillvalue='')])
</code></pre>

python