Issue with Sum of Odd Numbers Code
<p>I know that there are other post on this and I have referred to them but I can't seem to figure this one out.</p>

<p>This code gives me the value 2500, just 2500.</p>

<pre><code>sum = 0;
for i in range (1,100):
    if i % 2 == 1:
        sum = sum + i;
print (sum)
</code></pre>

<p>However I want the range to vary. So I came up with this</p>

<pre><code>def odd_sum_n(n):
    sum = 0;
    for i in range(1,2*n):
        if i % 2 != 0:
            sum = sum + i;
    print (sum)

print(odd_sum_n(5))
</code></pre>

<p>But this code gives me the solution</p>

<pre><code>25
None
</code></pre>

<p>I kept trying to adjusting this code but I don't see why the "None" appears. I'm new to python so any help would be appreciated!</p>

<p>This program illustrates why you get <code>2500</code> printed</p>

<pre><code>def print2500():
  print(2500)

print2500()
</code></pre>

<p>And this one why you get <code>None</code> printed</p>

<pre><code>def doNotReturnAnything():
  n = 2500

print(doNotReturnAnything())
</code></pre>

<p>Combine both and you get exactly the same result as in the Opening Question :</p>

<pre><code>def print2500():
  print(2500)

print(print2500())
</code></pre>

<p>And finally the correct way to do it, have the function return a value.</p>

<pre><code>def return2500():
  return 2500

print(return2500())
</code></pre>

<p>Try them yourself!</p>

python