R. Sweet - 1 year ago 248

Python Question

Hey I have to make a program in python that takes input from the user for number of rows, columns, square side length, and three colors. Then the program must make a grid according to the number of rows and columns, and fill in the squares in an alternating checker pattern. I have it coded up to the point where it fills in the colors, and i was wondering if anyone could help me. Here's what i have so far:

`from turtle import *`

t = Turtle()

screen = t.getscreen()

rows = screen.numinput('Number of rows',

'How many rows shall there be?', 5, 1, 10)

columns = screen.numinput('Number of columns',

'How many columns shall there be?', 5, 1, 10)

side_length = screen.numinput('Length of square side',

'How long shall the square sides be?', 30, 10, 50)

first_color = screen.textinput('First color',

'What shall the first color be?')

second_color = screen.textinput('Second color',

'What shall the second color be?')

third_color = screen.textinput('Third color',

'What shall the third color be?')

square_color = ''

def draw_square():

t.begin_fill()

t.pendown()

t.forward(side_length)

t.left(90)

t.forward(side_length)

t.left(90)

t.forward(side_length)

t.left(90)

t.forward(side_length)

t.color(square_color)

t.end_fill()

t.penup()

t.color('black')

t.left(90)

t.forward(side_length)

def draw_board():

n = 1

for i in range(int(columns)):

draw_square()

for x in range(int(rows - 1)):

t.goto(0,side_length * n)

for i in range(int(columns)):

draw_square()

n += 1

for i in range(int(columns)):

for x in range(int(rows)):

if x + i % 3 == 0:

square_color = first_color

elif x + i % 3 == 1:

square_color = second_color

elif x + i % 3 == 2:

square_color = third_color

draw_board()

done()

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You have all the code you need, in fact too much. You just don't have it assembled correctly. You need to think of your program as a story and tell that story in the correct order of events such that the story makes sense. Below is my rework of your code to put things in a better order along with some style tweaks and code cleanup:

```
from turtle import Turtle, Screen
def draw_square(turtle, length, color):
turtle.color(color)
turtle.pendown()
turtle.begin_fill()
for _ in range(4):
turtle.forward(length)
turtle.left(90)
turtle.end_fill()
turtle.penup()
turtle.forward(length)
def draw_board(turtle, length, colors):
n = 0
for row in range(int(rows)):
turtle.goto(0, length * n)
for column in range(int(columns)):
square_color = colors[(column + row) % len(colors)]
draw_square(turtle, length, square_color)
n += 1
screen = Screen()
rows = screen.numinput('Number of rows', 'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns', 'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of side', 'How long shall the square sides be?', 30, 10, 50)
first_color = screen.textinput('First color', 'What shall the first color be?')
second_color = screen.textinput('Second color', 'What shall the second color be?')
third_color = screen.textinput('Third color', 'What shall the third color be?')
colors = [first_color, second_color, third_color]
turtle = Turtle()
turtle.penup()
draw_board(turtle, side_length, colors)
turtle.hideturtle()
screen.exitonclick()
```

**PRODUCES:**

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**