Python 中的随机颜色
Random colors in Python
我在运行此程序时遇到问题。输出应该用我拥有的 10 种颜色列表中的随机颜色随机填充每个方块,但每个方块仅填充初始颜色。我需要保持 pick_color()
的定义与教授的定义相同,因为他要求我们保持不变。仅供参考,每个正方形逐渐变小,首先绘制最大的正方形。
顺便说一句,我问是因为教授们鼓励我这样做,但是当我在我们学校的广场上发帖时,我并没有得到有益的帮助。我在 Whosebug 和其他资源中搜索了解决方案,但无济于事。
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
如果我能得到一两个提示就好了。我不是在寻找直接的答案。提前致谢!
pick_color
创建一个颜色列表,将它们打乱顺序,然后 returns 第一个颜色,您 use repeatedly。相反,在 top/global 级别定义该列表,并使用 random.choice
:
选择随机颜色
turtle.fillcolor(random.choice(colors))
你必须在循环中调用它的函数,你第一次调用它,所以它永远不会改变。
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
输出:
我同意@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()
,尽管不是您教授使用的方式,但会跟踪返回的颜色以确保上一次随机播放的最后一种颜色不是第一种新洗牌的颜色:
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()
我相信这会产生比您显示的相邻方块颜色相同的结果更好的结果:
我在运行此程序时遇到问题。输出应该用我拥有的 10 种颜色列表中的随机颜色随机填充每个方块,但每个方块仅填充初始颜色。我需要保持 pick_color()
的定义与教授的定义相同,因为他要求我们保持不变。仅供参考,每个正方形逐渐变小,首先绘制最大的正方形。
顺便说一句,我问是因为教授们鼓励我这样做,但是当我在我们学校的广场上发帖时,我并没有得到有益的帮助。我在 Whosebug 和其他资源中搜索了解决方案,但无济于事。
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
如果我能得到一两个提示就好了。我不是在寻找直接的答案。提前致谢!
pick_color
创建一个颜色列表,将它们打乱顺序,然后 returns 第一个颜色,您 use repeatedly。相反,在 top/global 级别定义该列表,并使用 random.choice
:
turtle.fillcolor(random.choice(colors))
你必须在循环中调用它的函数,你第一次调用它,所以它永远不会改变。
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
输出:
我同意@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()
,尽管不是您教授使用的方式,但会跟踪返回的颜色以确保上一次随机播放的最后一种颜色不是第一种新洗牌的颜色:
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()
我相信这会产生比您显示的相邻方块颜色相同的结果更好的结果: