# How to cycle through different colours and assign them to already drawn objects? [closed]

I've been given the task to draw a patchwork pattern using Python. I need to get a width and height from the user, both must be greater than 3 but less than 10, and 4 colours, that cannot be the same. I have written the code for that fine. But i need to draw 2 different types of patterns. One pattern is all along the edges, only 1 patch deep, and the second pattern fills in the centre squares. Now, the code i've used to draw this is repeating the first patch along the top, the bottom, and then each side, and then repeating the second pattern throughout the middle. But now i have to assign each different individual patch a colour, cycling through the given colours, so that it starts with the first, cycles through them all, and starts with the first again.

My question is, i have no idea how to cycle through the colours, because the order will be different line by line. Is there a good efficient way of doing it with what i've written so far, ie. drawing an individual patch and repeating it over lines, and if so what is the best way?

from graphics import *

def main():
width, height = getDimensions()
colour1, colour2, colour3, colour4 = getColours()
win = drawGraphWin(width, height)
drawPattern(win, width, height, colour1, colour2, colour3, colour4)

def getDimensions():
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while True:
try:
width = int(width)
break
except ValueError:
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while width < 4 or width > 9:
width = eval(input("How many patches, between 4 and 9, would you like \
horizontally? :"))
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while True:
try:
height = int(height)
break
except ValueError:
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while height < 4 or height > 9:
height = eval(input("How many patches, between 4 and 9, would you like \
vertically? :"))
return width, height

def getColours():
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid1(colour1) == False:
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid2(colour1, colour2) == False:
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid3(colour1, colour2, colour3) == False:
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid4(colour1, colour2, colour3, colour4) == False:
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
return colour1, colour2, colour3, colour4

def valid1(colour1):
if any( [colour1 == "red", colour1 == "green", colour1 == "blue", \
colour1 == "yellow", colour1 == "magenta", colour1 == "orange", \
colour1 == "cyan"]):
return True
else:
return False

def valid2(colour1, colour2):
if any( [colour2 == "red", colour2 == "green", colour2 == "blue", \
colour2 == "yellow", colour2 == "magenta", colour2 == "orange", \
colour2 == "cyan"]):
if colour2 == colour1:
return False
else:
return True
else:
return False

def valid3(colour1, colour2, colour3):
if any( [colour3 == "red", colour3 == "green", colour3 == "blue", \
colour3 == "yellow", colour3 == "magenta", colour3 == "orange", \
colour3 == "cyan"]):
if any( [colour3 == colour2, colour3 == colour1]):
return False
else:
return True
else:
return False

def valid4(colour1, colour2, colour3, colour4):
if any( [colour4 == "red", colour4 == "green", colour4 == "blue", \
colour4 == "yellow", colour4 == "magenta", colour4 == "orange", \
colour4 == "cyan"]):
if any( [colour4 == colour3, colour4 == colour2, colour4 == colour1]):
return False
else:
return True
else:
return False

def drawGraphWin(width, height):
win = GraphWin("CW PatchWork Deisgn", width*100, height*100)
win.setCoords(0.0,0.0,4*width,3*height)
for i in range(width):
vLineN = Line(Point(i*4, 0), Point(i*4, height*3))
vLineN.draw(win)
for j in range(height):
hLineN = Line(Point(0, j*3), Point(width*4, j*3))
hLineN.draw(win)
return win

def drawPattern(win, width, height, colour1, colour2, colour3, colour4):
for widthNo in range(width):
drawPatch1(win, widthNo, 0, colour1, colour2, colour3, colour4)
for widthNo in range(width):
drawPatch1(win, widthNo, height-1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, 0, heightNo+1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, width-1, heightNo+1, colour1, colour2, colour3, colour4)
for innerNoH in range(width-2):
innerWidth = innerNoH +1
for innerNoV in range(height-2):
innerHeight = height - 2 - innerNoV
drawPatch2(win, innerWidth, innerHeight, colour1, colour2, colour3,\
colour4)
win.getMouse()
win.close()

def drawPatch1(win, widthNo, height, colour1, colour2, colour3, colour4):
for i in range(6):
vLineN = Line(Point((0.8*(i))+widthNo*4, 0+height*3), \
Point((0.8*(i))+widthNo*4, 3+height*3))
vLineN.draw(win)
hLineN = Line(Point(0+widthNo*4, (0.6*(i))+height*3), \
Point(4+widthNo*4, (0.6*(i))+height*3))
hLineN.draw(win)
if i == 0:
for j in range(5):
hiMessage = drawHiMessage(0.4+widthNo*4, (0.3+(j*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)
elif i == 1:
for k in range(5):
hiMessage = drawHiMessage(1.2+widthNo*4, (0.3+(k*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour2)
vLineN.setFill(colour2)
hLineN.setFill(colour2)
elif i == 2:
for l in range(5):
hiMessage = drawHiMessage(2+widthNo*4, (0.3+(l*0.6))+height*3, \
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour3)
vLineN.setFill(colour3)
hLineN.setFill(colour3)
elif i == 3:
for m in range(5):
hiMessage = drawHiMessage(2.8+widthNo*4, (0.3+(m*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour4)
vLineN.setFill(colour4)
hLineN.setFill(colour4)
elif i == 4:
for n in range(5):
hiMessage = drawHiMessage(3.6+widthNo*4, (0.3+(n*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)

def drawHiMessage(x, y, win, colour1, colour2, colour3, colour4):
hiMessage = Text(Point(x, y), "hi!")
hiMessage.draw(win)
hiMessage.setSize(7)
return hiMessage

def drawPatch2(win, innerNoH, height, colour1, colour2, colour3, colour4):
for i in range(4):
x = (0.5+i)+innerNoH*4
for j in range(3):
y = (2.2-j)+height*3
sail = drawBoat(x, y, win, colour1, colour2, colour3, colour4)
if i == 0:
sail.setFill(colour1)
elif i == 1:
sail.setFill(colour2)
elif i ==2:
sail.setFill(colour3)
elif i == 3:
sail.setFill(colour4)

def drawBoat(x, y, win, colour1, colour2, colour3, colour4):
sail = Polygon(Point(x-0.5,y), Point(x,y+0.8), Point(x+0.5,y))
hull = Polygon(Point(x-0.5,y-0.1), Point(x+0.5,y-0.1), Point(x+0.3,y-0.2), \
Point(x-0.3,y-0.2))
mast = Line(Point(x,y), Point(x,y-0.1))
sail.draw(win)
hull.draw(win)
mast.draw(win)
hull.setFill("white")
return sail

main()
-
I don't think your question really fits on this site. You are basicly asking for help on a programming a piece of code. This fits more on StackOverflow. This site is for requests to improve code that you think works but would like to improve. – Rutix Nov 19 '12 at 12:30
Sorry for that, but they sent me over here and said it wasn't really for them. I think i'm on to something now anyway. But thanks for looking. – AlexJonBarry Nov 19 '12 at 13:26
Your code is littered with massive amounts of repetition. That's a really bad sign. – Lattyware Nov 19 '12 at 16:25

## closed as off topic by Brian Reichle, Winston Ewert♦Nov 20 '12 at 14:56

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.