2017-09-23 29 views
0

我的代码在屏幕上打印一个点后,它不会再运行。这就是它告诉我,在终端后,我关闭龟屏幕:我的定义语句只会运行一次

Traceback (most recent call last): 
    File "/Users/Benjamin/Desktop/Space Digital Scene.py", line 33, in <module> 
    star(size, x, y, color) 
    File "/Users/Benjamin/Desktop/Space Digital Scene.py", line 12, in star 
    drawer.begin_fill() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 3322, in begin_fill 
    self._fillitem = self.screen._createpoly() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/turtle.py", line 497, in _createpoly 
    return self.cv.create_polygon((0, 0, 0, 0, 0, 0), fill="", outline="") 
    File "<string>", line 1, in create_polygon 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2492, in create_polygon 
    return self._create('polygon', args, kw) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2474, in _create 
    *(args + self._options(cnf, kw)))) 
_tkinter.TclError: invalid command name ".!canvas" 

我的代码是

import turtle 
import random 

def star(size, X, Y, color): 
    drawer.goto(X, Y) 
    drawer.color(color) 
    drawer.begin_fill() 
    drawer.circle(size) 
    drawer.end_fill() 

hex = ["blue","red","yellow","white"] 

screen = turtle.Screen() 
turtle.bgcolor("black") 
drawer = turtle.Turtle() 
drawer.speed("fastest") 

x = random.randint(-300,301) 
y = random.randint(-300,301) 
color = random.choice(hex) 
size = random.randint(1,6) 

a = 1 
b = 100 

while True: 
    if a <= b: 
     star(size, x, y, color) 
     drawer.hideturtle() 
     a + 1 
     continue 
    else: 
     break 

screen.mainloop() 

回答

1

您必须分配新a或使用a += 1也移动内部的一些线条while回路每次执行它们:

while True: 
    if a <= b: 
     x = random.randint(-300, 301) 
     y = random.randint(-300, 301) 
     star(size, x, y, color) 
     drawer.hideturtle() 
     a += 1 

     continue 
    else: 
     break 

screen.mainloop() 
+0

谢谢是的,它的工作,我明白了,谢谢。 – Kabdmenrs