I am getting the output for the program but not in the required way?
<p>Write a Python function that accepts a string as an input.
The function must return the sum of the digits 0-9 that appear in the string, ignoring all other characters.Return 0 if there are no digits in the string.</p>

<p>my code:</p>

<pre><code>user_string = raw_input("enter the string: ")
new_user_string = list(user_string)
addition_list = []
for s in new_user_string:
    if ( not s.isdigit()):
        combine_string = "".join(new_user_string)
        print ( combine_string)
    else:
        if ( s.isdigit()):
            addition_list.append(s)
            test = "".join(addition_list)
            output = sum(map(int,test))
            print ( output )
</code></pre>

<p>the output should be:</p>

<pre><code>Enter a string: aa11b33
8
</code></pre>

<p>my output:</p>

<pre><code>enter the string: aa11b33
aa11b33
aa11b33
1
2
aa11b33
5
8
</code></pre>

<p>It should be </p>

<pre><code>user_string = raw_input("enter the string: ")
new_user_string = list(user_string)
addition_list = []
for s in new_user_string:
    if ( not s.isdigit()):
        combine_string = "".join(new_user_string)
    else:
        if ( s.isdigit()):
            addition_list.append(s)
            test = "".join(addition_list)

output = sum(map(int,addition_list))
print output
</code></pre>

<p>You were getting the output you were for two reasons. </p>

<ol>
<li>In the if statement, you were telling it to print the string that was originally inputted when it came across a non-digit. This makes perfect sense as you look at your output - the string is printed when it sees a, the string is printed when it sees the second a, and the string is not printed, not printed (for the ones) and then the string is printed for the last time with the b.</li>
<li>You were printing the output as the for loop incremented through the list, meaning it printed the total each time. Moving the output variable and print statement outside the for loop fixed the problem.</li>
</ol>

python python-2.7 python-3.x ipython