我在让这个程序工作时遇到了问题。输出应该从我拥有的10种颜色的列表中随机填充每个正方形的颜色,但每个正方形仅填充初始颜色。我需要保持定义os pick_color()
与教授一样,因为他要求我们保持相同。仅供参考,每个方格逐渐变小,最大的方格首先被拉出。
import turtle
import random
squares = input("How many squares should I draw (whole numbers): ")
squares_int = int(squares)
def pick_color():
colors = ["blue","black","brown","red","yellow","green","orange","beige","turquoise","pink"]
random.shuffle(colors)
return colors[0]
random_color = pick_color()
print(random_color)
length = 400
x = -200
y = 200
for i in range(squares_int):
turtle.fillcolor(random_color)
turtle.pensize(5)
turtle.penup()
turtle.setx(x)
turtle.sety(y)
turtle.pendown()
turtle.down()
turtle.begin_fill()
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.end_fill()
turtle.up()
length = length - 30
x = x + 15
y = y - 15
如果我得到一两个提示,那就太棒了。我不是在寻找直接的答案,至少现在还没有。提前致谢!
答案 0 :(得分:1)
你必须在循环中调用它的函数,你第一次调用它,它永远不会改变。
import turtle
import random
squares = input("How many squares should I draw (whole numbers): ")
squares_int = int(squares)
def pick_color():
colors = ["blue","black","brown","red","yellow","green","orange","beige","turquoise","pink"]
random.shuffle(colors)
return colors[0]
length = 400
x = -200
y = 200
for i in range(squares_int):
random_color = pick_color()
turtle.fillcolor(random_color)
turtle.pensize(5)
turtle.penup()
turtle.setx(x)
turtle.sety(y)
turtle.pendown()
turtle.down()
turtle.begin_fill()
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.end_fill()
turtle.up()
length = length - 30
x = x + 15
y = y - 15
输出:
答案 1 :(得分:0)
pick_color
创建一个颜色列表,将它们混洗,然后返回第一个颜色,use repeatedly。相反,在顶部/全局级别定义该列表,并使用random.choice
选择随机颜色:
turtle.fillcolor(random.choice(colors))
答案 2 :(得分:0)
我同意@ TigerhawkT3(+1),你教授对pick_color()
的实施是废话。但我不相信random.choice()
,也不是你教授滥用random.shuffle()
的方式,是最好的选择。两者的问题是你可以在连续的调用中获得相同的颜色,这是你在正方形中绘制正方形时不想要的颜色:
>>> import random
>>> COLORS = ['red', 'blue', 'green', 'yellow', 'black', 'pink', 'gold', 'violet', 'orange', 'magenta', 'cyan']
>>> for _ in range(10):
... print(random.choice(COLORS))
...
green
pink
red
black
violet
orange
orange
violet
yellow
yellow
>>>
我仍然使用random.shuffle()
,虽然不是你教授的方式,但是通过跟踪返回颜色的附加修复来确保前一个shuffle的最后颜色不是第一个颜色新洗牌:
import turtle
import random
COLORS = ["blue", "black", "brown", "red", "yellow", "green", "orange", "beige", "turquoise", "pink"]
def pick_color(colors=[], previous=[None]): # intentionally dangerous default values
if not colors:
colors.extend(COLORS)
random.shuffle(colors)
if colors[-1] == previous[0]:
colors.insert(0, colors.pop())
previous[0] = colors.pop()
return previous[0]
squares = input("How many squares should I draw (whole numbers): ")
squares_int = int(squares)
length = 400
x = -200
y = 200
turtle.pensize(5)
for i in range(squares_int):
random_color = pick_color()
turtle.fillcolor(random_color)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
for _ in range(4):
turtle.forward(length)
turtle.right(90)
turtle.end_fill()
length -= 30
x, y = x + 15, y - 15
turtle.done()
我相信这会产生比使用相同颜色的相邻正方形显示的结果更好的结果: