Reputation: 95
I'm having a problem getting this program to work. The output is supposed to randomly fill each square with a random color from the list of 10 colors that I have, but each square is filled with the initial color only. I need to keep the definition of pick_color()
the same as the professor's as he asked us to keep it the same. FYI also, each square gets incrementally smaller, with largest one getting drawn first.
BTW, I am asking because the professors encouraged me to, yet I didn't receive help that was beneficial when I posted on our school's piazza. And I have searched StackOverFlow and other sources for a solution, but to no avail.
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
It would be great if I just got a hint or two. I'm not looking for a straight-up answer. Thanks in advance!
Upvotes: 1
Views: 13893
Reputation: 41872
I agree with @TigerhawkT3 (+1) that your professor's implementation of pick_color()
is crap. But I don't believe that random.choice()
, nor your professor's way of abusing random.shuffle()
, is the best option. The issue with both is you can get the same color on successive calls which is what you don't want when drawing squares within squares:
>>> 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
>>>
I'd still use random.shuffle()
, though not the way your professor did, with the addtional fix of keeping track of the returned color to make sure the last color of the previous shuffle isn't the first color of the new 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()
I believe this produces a better result than the one you displayed with adjacent squares with the same color:
Upvotes: 1
Reputation: 243947
The function you must call it inside the loop, you call it the first time so it will never change.
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
Output:
Upvotes: 1
Reputation: 49318
pick_color
creates a list of colors, shuffles them, then returns the first one, which you use repeatedly. Instead, define that list at the top/global level, and pick a random color with random.choice
:
turtle.fillcolor(random.choice(colors))
Upvotes: 0