I would say that it is because

```
type(d)
```

does not return the same thing in the two cases. There are different forms of integers in Sage, and it can be tricky from time to time : there are for instance "python" integers (i.e. int(6)) and Sage integers (i.e. Integer(6)).

The range function, in particular, only returns Python integers

```
sage: int(9)/int(4)
2
```

While you probably want to use Sage integers instead, which knows when they should consider themselves as rational numbers

```
sage: Integer(9)/Integer(4)
9/4
```

Sooooooo if you want to change your code a bit, I would say that you can fix it by replacing {{{range(something)}}} by {{{srange(something)}}}. srange returns Sage integers :-)

```
sage: map(type,range(4))
[int, int, int, int]
sage: map(type,srange(4))
[sage.rings.integer.Integer,
sage.rings.integer.Integer,
sage.rings.integer.Integer,
sage.rings.integer.Integer]
```

Nathann