Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:14:13+00:00 2026-06-14T12:14:13+00:00

I’ve been given the task to draw a patchwork pattern using Python. I need

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:14:15+00:00Added an answer on June 14, 2026 at 12:14 pm

    You can use itertools.cycle to go through all the colors repeatedly. So let’s say that you have all your patches in a list called patches, you could do something like the following

    import itertools
    colors = ['red','blue','green','yellow']
    patches = ['a','b','c','d','e','f','g','h','i','j']
    
    for patch, color in itertools.izip(patches, itertools.cycle(colors)):
        # color a patch
        print 'Colour', patch, 'as', color 
    

    prints out

    Colour a as red
    Colour b as blue
    Colour c as green
    Colour d as yellow
    Colour e as red
    Colour f as blue
    Colour g as green
    Colour h as yellow
    Colour i as red
    Colour j as blue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.