我想以3种颜色绘制圆圈。但在此代码中,用于绘制圆圈,输出没有颜色:
import turtle
window=turtle.Screen()
tess= turtle. Turtle()
import random
def getColor():
color=random.randint(1,3)
if color==1:
color="red"
elif color==2:
color=="yellow"
elif color==3:
color=="blue"
return color
print (random.randint(1,3))
def drawFace (x,y):
tess.penup()
tess.goto(x+5,y+10)
tess.circle(10)
tess.goto(x+15,y+10)
tess.circle(10)
tess.pendown()
答案 0 :(得分:0)
在getColor()
函数中,当它为黄色或蓝色时,您没有分配给color
变量 - 您使用的是双等号。这是固定版本:
def getColor():
color=random.randint(1,3)
if color==1:
color="red"
elif color==2:
color="yellow"
elif color==3:
color="blue"
return color
其次,你在drawFace()
的开头拾起了笔,并且在完成之前从未放下它!这是修复:
def drawFace (x,y):
tess.penup()
tess.goto(x+5,y+10)
tess.pendown()
tess.circle(10)
tess.penup()
tess.goto(x+15,y+10)
tess.pendown()
tess.circle(10)
答案 1 :(得分:0)
您不需要选择随机数来索引颜色,您可以使用random.choice()
直接选择一个。您需要致电GetColor()
并应用您通过tess.pencolor()
选择的颜色我们也倾向于考虑根据他们的中心定位圈子,但Python龟并不是我们需要(明确地)在你的代码中(隐式地)调整它:
from turtle import Turtle, Screen
import random
RADIUS = 10
def getColor(turtle):
choice = turtle.pencolor()
while choice == turtle.pencolor():
choice = random.choice(["red", "green", "blue"])
return choice
def drawFace(turtle, x, y):
turtle.pencolor(getColor(turtle))
turtle.penup()
turtle.goto(x, y - RADIUS)
turtle.pendown()
turtle.circle(RADIUS)
tess = Turtle()
drawFace(tess, 5, 0)
drawFace(tess, 15, 0)
screen = Screen()
screen.exitonclick()