#Heres到目前为止我所拥有的。 colorCircle(myTurtle)似乎不起作用,我真的不知道该怎么做。有点业余,任何提示都将不胜感激。
def drawPolygon(myTurtle,sideLength,numSides):
turnAngle = 360/numSides
for i in range(numSides):
myTurtle.forward(sideLength)
myTurtle.right(turnAngle)
def drawCircle(myTurtle,radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
drawPolygon(myTurtle, sideLength, 360)
def colorCircle(myTurtle):
x = random.random()
y = random.random()
myTurtle.up()
myTurtle.goto(x,y)
myTurtle.down()
radius = random.random()
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
random.randint(0,9)
myTurtle.begin_fill(color)
drawCircle(myTurtle)
答案 0 :(得分:1)
此:
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
random.randint(0,9)
myTurtle.begin_fill(color)
相当于:
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
4
myTurtle.begin_fill(color)
因为您没有将随机数分配给变量,例子中为4,所以随机数被丢弃。然后将整个颜色列表作为参数传递给begin_fill()。
要获得随机颜色,您可以这样做:
colors = ['white', 'yellow'....] #Note the change here.
rand_num = random.randint(0,9)
selected_color = colors[rand_num]
但是,python使事情easier比那个:
import random
colors = ['white', 'yellow'....]
selected_color = random.choice(colors) #Pick a random element from the colors list
编辑:=============
但是,您尝试将参数传递给begin_fill()
函数,但begin_fill()
不接受任何参数。默认情况下,begin_fill()
使用黑色作为填充颜色。要将填充颜色设置为其他颜色,请使用以下功能之一:
fill_color('red') #Sets fill color.
color('red') #Sets both pen and fill color.
以下是您可以做的一些示例:
import turtle
import random
def drawPolygon(myTurtle,sideLength,numSides):
turnAngle = 360/numSides
for i in range(numSides):
myTurtle.forward(sideLength)
myTurtle.right(turnAngle)
def drawCircle(myTurtle, radius, fill_color="blue"):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
myTurtle.color(fill_color) #color() sets pen and fill color
myTurtle.begin_fill() #The next shape that is drawn will be filled.
drawPolygon(myTurtle, sideLength, 360)
myTurtle.end_fill() #Disable filling.
def getRandomColor():
colors = ['yellow', 'green', 'blue']
rand_color = random.choice(colors)
return rand_color
rand_color = getRandomColor()
drawCircle(turtle, 40, rand_color)
turtle.exitonclick()
或者你可以这样做:
import turtle
import random
def drawPolygon(myTurtle,sideLength,numSides):
turnAngle = 360/numSides
for i in range(numSides):
myTurtle.forward(sideLength)
myTurtle.right(turnAngle)
def getRandomColor():
colors = ['yellow', 'green', 'blue']
rand_color = random.choice(colors)
return rand_color
def drawCircle(myTurtle, radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
rand_color = getRandomColor()
myTurtle.color(rand_color) #color() sets pen and fill color
myTurtle.begin_fill()
drawPolygon(myTurtle, sideLength, 360)
myTurtle.end_fill()
drawCircle(turtle, 40)
turtle.exitonclick()
<强> =========== 强>
最后,颜色不是列表的好名称。列表名称应为复数,例如:颜色,数字,单词;然后你可以编写如下所示的循环:
for color in colors:
...
for number in numbers:
...
for word in words:
...
答案 1 :(得分:0)
首先,您不能将随机数分配给任何内容
random.randint(0,9)
#You just say okay give me random number from 0 to 9 and then don't do anything with it
第二个问题是您无法填写begin_fill(color)
,但您可以输入begin_fill()
,但默认填充颜色为黑色。所以你需要做的是这样的事情:
color = [('white',0),('yellow',1),('green',2),('blue',3),('purple',4),('red',5),('black',6),('magenta',7),('pink',8),('brown',9)]
randomPos = random.randint(0,9) #We randomly choose which color will it be
myTurtle.fill_color(color[ randomPos ]) #We set fill color to color we previously chose
myTurtle.begin_fill() #We start to fill
我希望我有所帮助,如果您有任何疑问,请问我。