Variable n as an upper bound of a for loop
<p>I wanted to calculate the sum of (1+1+1+...+1)^2 with a for loop, but with a variable n as the upper bound so that the sum results in a function f(n) = n^2. With a non-negative integer x as the input, the loop could easily be coded as</p>

<pre><code>p==0
def f(x):
    global p
    for i in range(0,x):
        p=p+1
    return p**2
</code></pre>

<p>I wanted to feed a string n to this sort of loop. Is this doable (with or without modification)? Do I have to do string-converting hijinks a la SymPy?</p>

<p>edit: wow I worded this problem <strong>really</strong> poorly. guess sleep is important after all</p>

<p>Suppose I have a function f(x), where</p>

<pre><code>f(1) = 1  
f(2) = f(1+1) = (1+1)^2 = 4  
f(3) = f(1+2*1) = (1+2)^2 = 9
</code></pre>

<p>Then, f(n) would be</p>

<pre><code>f(n) = f(1+(n-1)*1) = (1+(n-1))^2 = n^2
</code></pre>

<p>If I were to calculate the result with a for loop above (which is a mess due to sleep deprivation), I'd enter f(1), f(2), f(3),... to the for loop above. I'm asking if I can enter n to the for loop above, with or without modification, and get n^2.</p>

<p>(The relation between f(1), f(2),... here isn't the same as the problem I have right now - I'm just wondering whether this idea is possible or not, and if so, how. I wouldn't learn anything if I ask the question directly, would I?)</p>

<p>edit2: An example of input/output with the algorithm above:</p>

<p>(Integer input)</p>

<pre><code>f(1) = 1^2
f(2) = 2^2
f(423) = 423^2
</code></pre>

<p>(Float input)</p>

<pre><code>f(0.5) = (0.5)^2
f(1.5) = (1.5)^2
f(234.23) = (234.23)^2
</code></pre>

<p>(String input - without assigning integer/float values to these strings first)</p>

<pre><code>f(a) = a^2
f(df) = (df)^2
f(the_speed_of_an_unladen_swallow) = (the_speed_of_an_unladen_swallow)^2
</code></pre>

<p>What I'm aiming for is a modification for the loop above so that I can do this, for f(a) where f is the function above:</p>

<pre><code>def g(a):
    return f(a)
</code></pre>

<p>or the equivalent thereof.</p>

<p>I'm just going to leave this here since I look less like a sleep-deprived uni student and more like a sleep-deprived retarded uni student right now.</p>

<p>You wrote:</p>

<blockquote>
  <p>(String input - without assigning integer/float values to these strings first)</p>

<pre><code>f(a) = a^2
f(df) = (df)^2
f(the_speed_of_an_unladen_swallow) = (the_speed_of_an_unladen_swallow)^2
</code></pre>
</blockquote>

<p>So, you have variables (<code>a</code>, <code>df</code>, <code>the_speed_of_an_unladen_swallow</code>), which do not yet contain a (good) value at the time you call <code>f</code>. Later, they would have a numerical value, and you'd like to loop that many times in your function.</p>

<p>As variables with a primitive data type are passed by value, you would have to pass the name of the variable instead, to do something similar.</p>

<p>Maybe this is something you are looking for?</p>

<pre><code>def f(x):
    def localF():
        return globals()[x]**2  # loop could be here, but this is the essence
    return localF

# n does not have its value yet   
n = None

# call f: it should not produce the result yet, as n is not correctly set yet
g = f('n')

# set n to its intended value
n = 5;

# Call the function we got from f:
print (g());

# output is 25.
</code></pre>

python iteration