I don't understand why i get difference answer by change put line if on in code
<p>I would write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example <code>s = 'azcbobobegghakl'</code>
will print out <code>'beggh'</code>.</p>

<pre><code>s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:
  if alpha &lt; word:
        word ='a'
        sen = ''   
  if alpha &gt;= word:   
        word = alpha
        sen += alpha
        if len(result) &lt;len(sen):
            result = sen

print('Longest substring in alphabetical order is: '+ result)
</code></pre>

<p>Answer is </p>

<pre><code>Logest substring in alphabetical order is: beggh
</code></pre>

<p>But just put code on different line. I got a wrong answer!!!</p>

<pre><code>s = 'azcbobobegghakl'
result = ''
word ='a'
sen = ''
for alpha in s:  
  if alpha &gt;= word:   
       word = alpha
       sen += alpha
       if len(result) &lt;len(sen):
           result = sen
  if alpha &lt; word:
     word ='a'
     sen = '' 
print('Longest substring in alphabetical order is: '+ result)
</code></pre>

<p>Answer </p>

<pre><code>Longest substring in alphabetical order is: eggh
</code></pre>

<p>The problem drive me mad!!!</p>

<p>Ok, let's say you got a couple of functions f1 and f2 and you're tracking every iteration of your for loop like this:</p>

<pre><code>def f1(s):
    result = ''
    word = 'a'
    sen = ''
    for index, alpha in enumerate(s):
        if alpha &lt; word:
            word = 'a'
            sen = ''
        if alpha &gt;= word:
            word = alpha
            sen += alpha
            if len(result) &lt; len(sen):
                result = sen

        print("{0}-{1} =&gt; {2}".format(index, alpha, result))

    return result


def f2(s):
    result = ''
    word = 'a'
    sen = ''
    for index, alpha in enumerate(s):
        if alpha &gt;= word:
            word = alpha
            sen += alpha
            if len(result) &lt; len(sen):
                result = sen
        if alpha &lt; word:
            word = 'a'
            sen = ''

        print("{0}-{1} =&gt; {2}".format(index, alpha, result))

    return result

s = 'azcbobobegghakl'
print('-' * 80)
print('f1: Longest substring in alphabetical order is: ' + f1(s))
print('-' * 80)
print('f2: Longest substring in alphabetical order is: ' + f2(s))
</code></pre>

<p>Now, running this script would produce:</p>

<pre><code>--------------------------------------------------------------------------------
0-a =&gt; a
1-z =&gt; az
2-c =&gt; az
3-b =&gt; az
4-o =&gt; az
5-b =&gt; az
6-o =&gt; az
7-b =&gt; az
8-e =&gt; az
9-g =&gt; beg
10-g =&gt; begg
11-h =&gt; beggh
12-a =&gt; beggh
13-k =&gt; beggh
14-l =&gt; beggh
f1: Longest substring in alphabetical order is: beggh
--------------------------------------------------------------------------------
0-a =&gt; a
1-z =&gt; az
2-c =&gt; az
3-b =&gt; az
4-o =&gt; az
5-b =&gt; az
6-o =&gt; az
7-b =&gt; az
8-e =&gt; az
9-g =&gt; az
10-g =&gt; egg
11-h =&gt; eggh
12-a =&gt; eggh
13-k =&gt; eggh
14-l =&gt; eggh
f2: Longest substring in alphabetical order is: eggh
</code></pre>

<p>As you can see, everything is the same till it reaches the iteration 9, with f1 result becomes <code>9-g =&gt; beg</code> while with f2 becomes <code>9-g =&gt; az</code>, that's how the change of order is affecting to the result, hope it makes sense now.</p>

python