Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the turtle library, write a function draw_grid(n,m): It’ll take two positi

ID: 3781673 • Letter: U

Question

Using the turtle library, write a function draw_grid(n,m):

It’ll take two positive ints n and m and draws an n × m grid of squares of the size of your choice, where n is the height in squares and m is the width in squares.

For example, draw_grid(6,5) would draw:

Set the title of the turtle window to be My n-by-m grid, replacing n and m with the dimensions from the function. For example, if the user called draw_grid(6,5), the title should be My 6-by-5 grid. (Remember, title() takes any string, and you can format strings using the %d formatting tools.

I am using jupyter console and jupyter notebook.

Okay below is what I have so far. When I import grid it works but then when I draw_grid (#,#), with the # being two random numbers selected by me, I get the error message "name 'draw_grid' is not defined." Can somebody please tell me where I went wrong and the correct way to format this. I'm still unfamilar with using turtle in def functions.

import turtle

def draw_grid (n,m):
"""Draws a grid using n as the height in squares and m as the width in squares
  
Arg:
n (int): postive integer
m (int): positive integer
  
Returns:
drawing: grid of both integers
"""
scrn = turtle.Screen()
scrn.title('My n-by-m grid')

Figure 1: Result of draw grid (6,5)

Explanation / Answer

#Attached python2.7 code

import turtle
def draw_grid(n,m):
   pen = turtle.Turtle()
   turtle.title("My " + str(n) + "-by-" + str(m) + " grid")
   for y in range(0,n):
   for x in range(0,m):
   pen.up()
   pen.setposition(x * 10, y * 10)
   pen.down()
   for s in range(0,4):
   pen.forward(10)
   pen.right(90)
draw_grid(2,3);