Python - Unsupported type(s) : range and range

前端 未结 4 1626
孤街浪徒
孤街浪徒 2021-01-18 01:55

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(         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 02:19

    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:

    def non_zero_range(lower, upper):
        ret = list(range(lower, upper))
        ret.remove(0)
        return ret
    

    and then change the second Racional() call argument from:

    coeff(choice(range(-30,0)+range(1,30)))
    

    to simply:

    coeff(choice(non_zero_range(-30,30)))
    

    You will have something that would work in both Python 2 and 3.

提交回复
热议问题