2017-11-21 3 views
0

私はそれを亀に表示しようとしていますが、 "shape_color is not defined"私の亀のコードにはエラーがあります。私は終わりに修正できないようです

私はbox_colorに変更した場合、私は私が残りはこのエラーを取得しない理由を、助けてください理解していない

line 14, in crescent
t.circle(width, extent=180, steps=none)
NameError: name 'none' is not defined

を取得します。

import random 
import turtle as t 

def crescent(x, y, width, height, shape_color): 
    t.fillcolor(shape_color) 
    t.penup() 
    t.goto(x, y) 
    t.pendown() 
    t.begin_fill() 
    t.circle(width, extent=180, steps=none) 
    t.endfill() 
    t.penup() 
    t.fillcolor("white") 
    t.end_fill() 
    t.penup() 
    t.fillcolor("white") 

def star(x, y, width, shape_color): 
    t.fillcolor(shape_color) 
    t.penup() 
    t.goto(x, y) 
    t.pendown() 
    t.begin_fill() 
    for s in range(5): 
     t.forward(width) 
     t.right(144) 
    t.end_fill() 
#-------------------------------------------------------------------- 

t.colormode(255) 
t.tracer(-1) 


for n in range(10): 
    x = random.randint(-200, 200) 
    y = random.randint(-200, 200) 

    r = random.randint(0, 255) 
    g = random.randint(0, 255) 
    b = random.randint(0, 255) 
    box_color = (r, g, b) 

    width = random.randint(5, 50) 
    height = random.randint(5, 50) 

    crescent(x, y, width, height, box_color) 
    star(x, y, width, height, shape_color) 

答えて

0

前年比多くのミスを持っています。あなたは少なくとも必要があります。

shape_color = box_color 
star(x, y, width, height, shape_color) 

- あなたはstep=noneを使用しますが、それは、上NNoneなければならないt.circle


step=Noneをスキップして、デフォルトでstep=Noneを使用します。
参照ドキュメント:turtle - circle

t.circle(width, extent=180) 

-

あなたはコマンドt.endfill()_を忘れてしまった - それはt.end_fill()

なければならない -

機能starは4つの引数を期待しdef star(x, y, width, shape_color):が、forループであなた5つの引数で実行するstar(x, y, width, height, shape_color)'. You have to remove h eight`

shape_color = box_color 
star(x, y, width, shape_color) 

全コード:繰り返しポリゴンを敷設する場合

import random 
import turtle as t 

def crescent(x, y, width, height, shape_color): 
    t.fillcolor(shape_color) 
    t.penup() 
    t.goto(x, y) 
    t.pendown() 
    t.begin_fill() 
    t.circle(width, extent=180) 
    t.end_fill() 
    t.penup() 
    t.fillcolor("white") 
    t.end_fill() 
    t.penup() 
    t.fillcolor("white") 

def star(x, y, width, shape_color): 
    t.fillcolor(shape_color) 
    t.penup() 
    t.goto(x, y) 
    t.pendown() 
    t.begin_fill() 
    for s in range(5): 
     t.forward(width) 
     t.right(144) 
    t.end_fill() 
#-------------------------------------------------------------------- 

t.colormode(255) 
t.tracer(-1) 


for n in range(10): 
    x = random.randint(-200, 200) 
    y = random.randint(-200, 200) 

    r = random.randint(0, 255) 
    g = random.randint(0, 255) 
    b = random.randint(0, 255) 
    box_color = (r, g, b) 

    width = random.randint(5, 50) 
    height = random.randint(5, 50) 

    crescent(x, y, width, height, box_color) 
    star(x, y, width, shape_color) 
+0

を定義されていないされていない手に入れた、これはたくさん手伝っどうもありがとうございました。 – weboolin

+0

完全なコードを実行すると、エラー 'NameError:name 'shape_color' not defined'が発生します。 – cdlane

+0

なぜ 't.tracer(-1)'という行を保持しましたか? 'tracer()'の '-1'引数は何をしますか?ドキュメンテーションによれば、引数は負ではない整数です。 – cdlane

0

私はコードに2つのチェックポイントがあると思います。 最初に、t.fillcolor(white)t.fillcolor("white")に変更してください。

第2に、crescent(x, y, width, height, shape_colorからcrescent(x, y, width, height, box_colorに変更します。あなたはshape_colorではなく、変数box_colorを割り当てているからです。あなたがstar(x, y, width, height, shape_color)を実行for内部

いますが、shape_colorをdefiveなかった -

:それは三日月」に関するただパラメータ

+0

だけで両方今、私は何も – weboolin

0

、あなたはを描く代わりのスタンピングを考えるかもしれません。刻印するとき、我々はそれ自身がその形を持つ各形状のためのカメを作ります。私たちは移動し、必要に応じてカメの色や大きさを決め、それをスタンプします。これには2つの利点があります。その描画ができないグラフィックオペレーションを活用する能力(のようなせん断):いくつかの方法で

from random import randint, random 
import turtle 

SHAPE_SIZE = 20 

def crescent(width): 
    turtle.begin_poly() 
    turtle.circle(width, extent=180) 
    turtle.end_poly() 

    return turtle.get_poly() 

def star(width): 
    turtle.begin_poly() 
    for _ in range(5): 
     turtle.forward(width) 
     turtle.right(144) 
    turtle.end_poly() 

    return turtle.get_poly() 

screen = turtle.Screen() 
width, height = screen.window_width()/2, screen.window_height()/2 

turtle.penup() # use the 'default' turtle to create the other two turtles 
turtle.hideturtle() 
turtle.setheading(90) 
turtle.speed('fastest') 

screen.register_shape('crescent', crescent(SHAPE_SIZE)) 
crescent_turtle = turtle.Turtle('crescent', visible=False) 
crescent_turtle.speed('fastest') 
crescent_turtle.penup() 

screen.register_shape('star', star(SHAPE_SIZE)) 
star_turtle = turtle.Turtle('star', visible=False) 
star_turtle.speed('fastest') 
star_turtle.penup() 

for _ in range(25): 
    x, y = randint(-width, width), randint(-height, height) 

    r, g, b = random(), random(), random() 
    shape_color = (r, g, b) 

    size = randint(5, 50) 

    heading = [0, 180][randint(0, 1)] 

    for tortoise in [crescent_turtle, star_turtle]: 
     tortoise.turtlesize(SHAPE_SIZE/size, outline=1) 
     tortoise.setheading(heading) 
     tortoise.color(shape_color) 
     tortoise.goto(x, y) 
     tortoise.stamp() 

screen.exitonclick() 

、それは(ロジックを簡素化する他のソリューションにstar()crescent()の私の定義を比較することができます。)また、制限もあります。単純なポリゴンで作業する必要があります。

enter image description here

関連する問題