Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

python - Python2.7 works but Python3.x throws error

def calculate(self):
        normals = []
        for f in self.sides:
            sideN= A(0.0, 0.0, 0.0)
            for i in range(0, len(f)):
                p1 = self.ver[f[(i - 1) % len(f)]]
                p2 = self.ver[f[i]]
                p3 = self.ver[f[(i + 1) % len(f)]]
                v1 = p2 - p1
                v2 = p2 - p3
                v33 = v2.cross(v1)
                v33 = v33.normalize()
                sideN += v33
            sideN = sideN.normalize()
            normals.append(sideN)
        return normals

this runs OK on python 2.7 but when I run in on python 3.8 it throws

 p1 = self.ver[f[(i - 1) % len(f)]]
TypeError: list indices must be integers or slices, not float

when I edit that part;

p1 = self.ver[f[(i - 1) % len(f)]]
p2 = self.ver[f[i]]
p3 = self.ver[f[(i + 1) % len(f)]]

to that

p1 = self.ver[int(f[(i - 1) % len(f)])]
p2 = self.ver[int(f[i])]
p3 = self.ver[int(f[(i + 1) % len(f)])]

then it throws;

p3 = self.ver[int(f[(i + 1) % len(f)])]
IndexError: list index out of range

I tried 2to3 but nothing is changed. What is the problem here?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...