Python程序错误

时间:2014-04-06 18:37:43

标签: python

我正在编写一个程序,需要找到所有初始条件对(x0,y0),其中x首先消失并绘制它们。方程是:

  

j = x-y + 100和   i = x + y-100

并且约束来自(0,200)x和y方向。

我的代码如下:

for x in range (0,201):
for y in range (0,201):
    c=0
    i=x
    j=y
while (x > 0 and y > 0):
    i=x-y+100
    j=x+y-100
    c=c+1
if i==0:
    plot(i,j)
if c==50:
    break

我的错误说:

    Traceback (most recent call last):                i=x-y+100
  File "", line 1, in <module>

  File "/tmp/tmpqTzdAj/___code___.py", line 3, in <module>
    exec compile(u'for x in range (_sage_const_0 ,_sage_const_201 ):\n    for y in range (_sage_const_0 ,_sage_const_201 ):\n        c=_sage_const_0 \n        i=x\n        j=y\n        while (x > _sage_const_0  and y > _sage_const_0 ):\n            i=x-y+_sage_const_100 \n            j=x+y-_sage_const_100 \n            c=c+_sage_const_1 \n        if i==_sage_const_0 :\n            plot([i],[j])\n        if c==_sage_const_50 :\n            break
  File "", line 11, in <module>

  File "/app/sage/sage-6.1.1/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 692, in wrapper
    return func(*args, **kwds)
  File "/app/sage/sage-6.1.1/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 537, in wrapper
    return func(*args, **options)
  File "/app/sage/sage-6.1.1/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1134, in plot
    G = _plot(funcs, *args, **kwds)
  File "/app/sage/sage-6.1.1/local/lib/python2.7/site-packages/sage/plot/plot.py", line 1236, in _plot
    funcs, ranges = setup_for_eval_on_grid(funcs, [xrange], options['plot_points'])
  File "/app/sage/sage-6.1.1/local/lib/python2.7/site-packages/sage/plot/misc.py", line 132, in setup_for_eval_on_grid
    range_steps = [abs(range[1] - range[0])/(p-1) for range, p in zip(ranges, plot_points)]
IndexError: list index out of range

2 个答案:

答案 0 :(得分:0)

情节(i,j)是否可能期待i&amp; j是坐标列表?看看如果你尝试会发生什么:

 plot([i],[j])

答案 1 :(得分:0)

最可能的问题是,当你需要一个数组时,你将整数传递给plot()

在某些内部,它会尝试获取数组的长度并失败,因为您无法获取整数的长度。

猜测,你的代码应该是

MAX_REPS = 50

good_pts = []

for x in range (0,201):
    for y in range (0,201):
        for c in range(MAX_REPS):
            x, y = x - y + 100, x + y - 100

            if y <= 0:
                break
            elif x <= 0:
                good_pts.append((x, y))
                break

然后在good_pts中绘制(x,y)点。