ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 28 Jun 2014 13:51:46 +0200Sage ranges in IP-Notebooks on CSMhttps://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/On cloud.sagemath
%load_ext sage
def f(n):
for k in (1..n):
print n
f(5)
gives
AttributeError
----> 1 f(Integer(5))
in f(n)
2
3 def f(n):
----> 4 for k in (1..n):
5 print n
AttributeError: 'float' object has no attribute 'n'
Does "%load_ext sage" not invoke the Sage preparser?
Sat, 28 Jun 2014 05:57:11 +0200https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/Comment by petropolis for <p>On cloud.sagemath</p>
<pre><code>%load_ext sage
def f(n):
for k in (1..n):
print n
f(5)
</code></pre>
<p>gives</p>
<pre><code>AttributeError
----> 1 f(Integer(5))
in f(n)
2
3 def f(n):
----> 4 for k in (1..n):
5 print n
AttributeError: 'float' object has no attribute 'n'
</code></pre>
<p>Does "%load_ext sage" not invoke the Sage preparser?</p>
https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?comment=16137#post-id-16137Create a new IPython notebook, copy the five lines given. That's all.Sat, 28 Jun 2014 13:51:46 +0200https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?comment=16137#post-id-16137Comment by slelievre for <p>On cloud.sagemath</p>
<pre><code>%load_ext sage
def f(n):
for k in (1..n):
print n
f(5)
</code></pre>
<p>gives</p>
<pre><code>AttributeError
----> 1 f(Integer(5))
in f(n)
2
3 def f(n):
----> 4 for k in (1..n):
5 print n
AttributeError: 'float' object has no attribute 'n'
</code></pre>
<p>Does "%load_ext sage" not invoke the Sage preparser?</p>
https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?comment=16138#post-id-16138Could you give more details on what exactly to do on cloud.sagemath.org to reproduce this?Sat, 28 Jun 2014 11:49:57 +0200https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?comment=16138#post-id-16138Answer by slelievre for <p>On cloud.sagemath</p>
<pre><code>%load_ext sage
def f(n):
for k in (1..n):
print n
f(5)
</code></pre>
<p>gives</p>
<pre><code>AttributeError
----> 1 f(Integer(5))
in f(n)
2
3 def f(n):
----> 4 for k in (1..n):
5 print n
AttributeError: 'float' object has no attribute 'n'
</code></pre>
<p>Does "%load_ext sage" not invoke the Sage preparser?</p>
https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?answer=16136#post-id-16136EDIT (knowing that this is happening in an IPython notebook):
If you put the line
%load_ext sage
in a cell by itself and evaluate it, and then put the rest in a second cell,
def f(n):
for k in (1..n):
print n
f(5)
then your example will work.
Note that, with everything in the same cell, as you had, if after getting the
`AttributeError` you re-evaluate the cell, then you get this warning:
The sage extension is already loaded. To reload it, use:
%reload_ext sage
but your function works and you get the expected output
5
5
5
5
5
The explanation is that `%load_ext sage` needs to be evaluated first before the preparser is activated letting you use any Sage syntax that violates standard Python syntax rules.
I would even put the definition of f and the command `f(5)` in different cells, so that you can work on the function definition separately from applying it to different values:
- while working on the definition of `f` (getting rid of any syntax errors), no use in trying to apply it.
- once `f` is defined, to try it with different arguments, no need to define it again each time.
-----
Original answer:
Are you writing these lines in a Python file?
The error message seems to indicate that `(1..n)` was parsed as trying to apply the method `.n` to the float `1.`, which is similar to other errors one can get when using certain Sage syntax shorthands involving a dot (`.`) in a Python file.
Besides `(1..n)` and `[1..n]`, I am thinking of syntax goodies to choose names for generators of an object as you define this object, such as in these examples:
R.<x,y,z> = PolynomialRing(QQ)
K.<a> = NumberField(x^2 - x - 1)
F.<a,b> = FreeGroup(2)
If used in a Python file, these will result in an error, because Python doesn't like these uses of the dot. In a Python file the examples above would become:
R = PolynomialRing(QQ,['x','y','z'])
x,y,z = R.gens()
P = PolynomialRing(QQ,'x')
x = P.gen()
K = NumberField(x**2 - x - 1)
a = K.gen()
F = FreeGroup(2)
a,b = F.gens()
(with appropriate import statements at the start of the file).Sat, 28 Jun 2014 11:47:51 +0200https://ask.sagemath.org/question/11073/sage-ranges-in-ip-notebooks-on-csm/?answer=16136#post-id-16136