I'm getting this strange error trying to run a script, the code appears to be correct but it seems python (3) didn't liked this part:
def function(x):
if integer:
return int(x)
else:
return x
non_nil = randrange(21)
d = dict([(randrange(101), Racional(coeff(randrange(-20,20)),
coeff(choice(range(-30,0)+\
range(1,30)))))
for k in range(non_nil)])
And i get the following error:
for k in range(non_nil)]) unsupported operand type(s) for +: 'range' and 'range'
I already tried to put the last four lines in a single one but python returns the same error.
As others have pointed out the problem is that in Python 3,
range()
returns an iterator not a list like it does in Python 2.Here' one workaround: Add something like the following function:
and then change the second
Racional()
call argument from:to simply:
You will have something that would work in both Python 2 and 3.
In the middle of your expression you do
range(-30,0) + range(1,30)
. This is causing the error because in Python 3range()
returns an iterator, not a list like in Python 2.x.One way to get this to work is to just convert each range to a list before adding:
Since it seems like you want to just exclude
0
from therange(-30, 30)
, you could also usefilter(None, range(-30, 30))
.Alternatively you could use
choice((1, -1)) * choice(range(1, 30))
, which is equivalent tochoice(list(range(-30, 0)) + list(range(1, 30)))
. (edit: actually the prior expression will not include -30 in the possibilities, not sure whether or not that is an issue).This is because Python 3
range
does not return alist
, like Python 2. This code was written for Python 2.This code, should be changed:
It should be changed to:
As other answers have said,
range()
being an iterator is your problem, however, a simpler (in my view) solution is to generate the list from-30
to30
then remove0
, rather than avoiding it:Naturally, if your ranges were more disparate, this might become unwieldy.