ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 17 Oct 2010 08:39:14 -0500Variable range()https://ask.sagemath.org/question/7728/variable-range/Hello,
I have the following code:
def f(a,b,c,x):
xtemp=a
for i in range(b,x):
xtemp=xtemp-xtemp*c
return xtemp
var('t')
plot(f(10000000,1,0.01,t),3,20)
and got the error
TypeError: range() integer end argument expected, got
sage.symbolic.expression.Expression.
I understand that range does nor support 'var' object, but how can I have a variable range in this kind of function?Sat, 16 Oct 2010 08:57:24 -0500https://ask.sagemath.org/question/7728/variable-range/Comment by Lynda for <p>Hello,
I have the following code:</p>
<pre><code>def f(a,b,c,x):
xtemp=a
for i in range(b,x):
xtemp=xtemp-xtemp*c
return xtemp
var('t')
plot(f(10000000,1,0.01,t),3,20)
</code></pre>
<p>and got the error</p>
<pre><code>TypeError: range() integer end argument expected, got
sage.symbolic.expression.Expression.
</code></pre>
<p>I understand that range does nor support 'var' object, but how can I have a variable range in this kind of function?</p>
https://ask.sagemath.org/question/7728/variable-range/?comment=22570#post-id-22570Thank you Niles it works well for my needsSun, 17 Oct 2010 08:39:14 -0500https://ask.sagemath.org/question/7728/variable-range/?comment=22570#post-id-22570Answer by niles for <p>Hello,
I have the following code:</p>
<pre><code>def f(a,b,c,x):
xtemp=a
for i in range(b,x):
xtemp=xtemp-xtemp*c
return xtemp
var('t')
plot(f(10000000,1,0.01,t),3,20)
</code></pre>
<p>and got the error</p>
<pre><code>TypeError: range() integer end argument expected, got
sage.symbolic.expression.Expression.
</code></pre>
<p>I understand that range does nor support 'var' object, but how can I have a variable range in this kind of function?</p>
https://ask.sagemath.org/question/7728/variable-range/?answer=11734#post-id-11734A [lambda function](http://www.secnetix.de/olli/Python/lambda_functions.hawk) might be the quickest workaround:
plot(lambda t: f(10000000,1,0.01,t),3,20)
And if you don't want to get the deprecation warning about `range` not taking `float` inputs, you could modify your function to:
def f(a,b,c,x):
xtemp=a
for i in range(b,ceil(x)):
xtemp=xtemp-xtemp*c
return xtemp
Sat, 16 Oct 2010 09:10:10 -0500https://ask.sagemath.org/question/7728/variable-range/?answer=11734#post-id-11734