Python代码挂起。在C ++中使用了相同的逻辑并且运行良好

时间:2014-01-08 04:01:21

标签: python matplotlib

我在C ++中使用while循环(显然没有matplotlib)运行相同的逻辑并完美地输出我的坐标。当我使用python它只是挂起。我以前从未见过这种情况。我有点失败,因为没有任何错误。我通常在C ++中使用我的逻辑,所以也许我在python中出错了?不确定。有什么想法吗?

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import math

variable924 = 0.0
variable921 = .0025
variable922 = .0450
variable923 = 5.0 + variable924
variable920 = 0
variable925 = 0
variable926 = 0
passes = 1
incpasses = passes
r = 0
degrees = 0

z = []
x = []
y = []

while (variable920 < 65):      
    variable925 = (1.0010-math.cos(variable920*(math.pi/180.00)))*variable922
    variable926 = (math.sin(variable920*(math.pi/180.00))-variable922)
    r = variable925 + variable921
    while (incpasses >= 1):
        xSet = (-variable926/passes)
        while (degrees <= 360):
            ySet = (math.sin(degrees * (math.pi / 180.00)))*r
            zSet = (math.cos(degrees * (math.pi / 180.00)))*r
            degrees = degrees + 30
            x.append(xSet)
            y.append(ySet)
            z.append(zSet)
        incpasses = incpasses - 1
        degrees = 0
incpasses = passes
variable920 = variable920 + variable923

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

1 个答案:

答案 0 :(得分:0)

嗯,当然,我会回答这个问题。

你的缩进错了。最外面的while循环具有基于variable920的条件,但variable920永远不会在循环中发生变化。这可能是您发布的代码中的缩进问题。通过将以下行缩进一级:

incpasses = passes
variable920 = variable920 + variable923

variable920的增量移动到最外层while循环的主体中,从而允许该循环终止。


在具有非暂停循环的未来情况下,您可能会发现包含sentinel print语句以在循环条件(如果有)中打印变量的值是有帮助的,因此您可以看到变量是如何在循环过程中改变(或不改变)。