为什么我在第6行上得到Traceback最近的通话记录和文件字符串

时间:2018-07-10 09:48:59

标签: python random

我的python代码在下面。每当我每次在编译器中运行此代码时,都会收到如下错误。

错误:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    turtle.setup(850, 850)
  File "<string>", line 6, in setup
  File "/usr/lib64/python2.7/lib-tk/turtle.py", line 3553, in Screen
    Turtle._screen = _Screen()
  File "/usr/lib64/python2.7/lib-tk/turtle.py", line 3569, in __init__
    _Screen._root = self._root = _Root()
  File "/usr/lib64/python2.7/lib-tk/turtle.py", line 458, in __init__
    TK.Tk.__init__(self)
  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1820, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

我的代码:

import random
import turtle

turtle.setup(850, 850)
turtle.penup()
turtle.speed('fastest')
turtle.tracer(0, 0)

x = 0
y = 0

for i in range(1, 16000):
    r = random.uniform(0, 1)

    if r < 0.1:
        nextX = 0
        nextY = float(0.16 * y)

        x = nextX
        y = nextY

    elif r < 0.88:
        nextX = float(0.85 * x * 0.04 * y)
        nextY = float(-0.04 * x * 0.85 * y + 1.6)

        x = nextX
        y = nextY

    elif r < 0.93:
        nextX = float(0.20 * x * -0.26 * y)
        nextY = float(0.23 * x * 0.22 * y + 1.6)

        x = nextX
        y = nextY

    else:
        nextX = float(-0.15 * x * 0.28 * y)
        nextY = float(0.26 * x * 0.24 * y + 0.44)

        x = nextX
        y = nextY    


    turtle.goto(x*40, y*40)
    turtle.dot(2, 'green')

turtle.done()

这是我第一次发现的未定义错误之一。 我怎样才能解决这个问题?该代码有什么问题。提前非常感谢您解决此问题

1 个答案:

答案 0 :(得分:0)

通常会发生这种情况,例如,如果您通过ssh在远程主机上启动脚本,因为没有显示并且无法设置DISPLAY环境变量。您在哪里运行脚本?