本文介绍了Python-不支持的类型:范围和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试运行脚本时遇到了一个奇怪的错误,该代码似乎是正确的,但是似乎python(3)不喜欢这部分:
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)])
我得到以下错误:
for k in range(non_nil)]) unsupported operand type(s) for +: 'range' and 'range'
我已经尝试将最后四行放在同一行中,但是python返回相同的错误.
I already tried to put the last four lines in a single one but python returns the same error.
推荐答案
这是因为Python 3 range
不返回list
,就像Python 2一样.此代码是为Python 2编写的.
This is because Python 3 range
does not return a list
, like Python 2. This code was written for Python 2.
此代码应更改:
range(-30,0) + range(1,30)
应更改为:
list(range(-30,0)) + list(range(1,30))
这篇关于Python-不支持的类型:范围和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!