Python: multiplying 'for i in range(..) iteration'
<p>So I have an assignment where I am required to return <code>inf</code> by multiplying <code>x</code> by 10 multiple times using a:</p>

<pre><code>for i in range(...)
</code></pre>

<p>so the main part of my code is:</p>

<pre><code>   def g(x):
      x = 10.*x
      for i in range(308):
          return x
</code></pre>

<p>and if I enter </p>

<pre><code>    &gt;&gt;&gt; g(3.)
    &gt;&gt;&gt; 30.0
</code></pre>

<p>I expected it to iterate it 308 times to the point where I get <code>inf</code>. Is there a line I can use that uses the number in the <code>for i in range(..)</code> to iterate the equation that many times?  For example:</p>

<pre><code>   def g(x):
       x = 2.*x
       for i in range(3):
       # the mystery line I need for it to work
           return x
   &gt;&gt;&gt; g(4.)
   &gt;&gt;&gt; 16.0
</code></pre>

<p>but it doesn't give me that. Rather it returns <code>8.0</code></p>

<p>Another way I did it that actually made it work was using print. But I don't think it's valid using a print statement in an assignment asking to <code>return inf</code>. </p>

<p>The reason it isn't iterating like you expect is because of the <code>return</code> statement. <code>return</code> exits from whatever procedure/function that you are running immediately and returns the value given to it. Here is a walk through of your first function, assuming it was called with the value of <code>4</code> as in <code>g(4.)</code>. </p>

<pre><code># the start of the function
# x = 10

# this line sets x = 100. I think you can see why.
x = 10. * x

# this sets i = 0 and begins to loop
for i in range(308):

    # this IMMEDIATELY returns from the function with the value
    # of x. At this point, x = 100. It does not continue looping!
    # Without this line, i would be set to 1, 2, 3, ..., 307 as 
    # you loop through the range. This is why it worked with print.
    return x
</code></pre>

<p>What you want instead of this is something that will accumulate the value of the repeated multiplications. You don't want to return anything at all within the loop, assuming you want every iteration to run. I will not give you the exact answer because this is homework but I will give a hint. You probably do not want <code>return</code> to be indented, as it is in the second bit of code you provided. You probably want something more like this:</p>

<pre><code>def g(x):
    x = 2 * x
    for i in range(3):
        # { your code here }
        # You want to accumulate the value!
    return x
</code></pre>

python python-3.x