The function has two parameters, one giving the side-length and the other giving the length of the shortest line. The function should draw as many lines of the spiral that fit inside the window

def spiral(lenght,side):
for i in range(1,side+1,5):
lenght.forward(i)
lenght.right(90)

I made a start of the code, please check the code and let me known if I am going on the right track. Also please find a solution to the problem

Recommended Answers

All 4 Replies

please use code tags for making your code readable and preserve indentation

Which graphics module are you using?

A quick example of a spiral using deque. It still has a few problems (works best with a square and prints too many) but shows the general concept.

from collections import deque

##========================================================================
def print_spiral( total_cols, total_rows ) :
   d_list = []  ## hold pointers to separate deque objects
   for j in range( 0, total_rows ) :
      d_list.append( deque( ) )

   ##  find center square
   this_row = total_rows / 2
   d_list[this_row].append( 1 )

   num_ctr = 1   ## the number to print
   incr = 1      ## 1 for first 10, 2 for second 10, etc
   num_cols = 1  ## number of columns to print for this row for this pass
   stop = total_rows
   x_dir = True  ## True  = + direction (right for x, down for y)
   y_dir = True  ## False = - direction (left for x, up for y)

   for j in range( 0, total_rows ) :
      x_dir = not x_dir

      ## for this row, increment the number and add to each column
      ## in this row, either left to right, or right to left
      for k in range( 0, num_cols ) :
         num_ctr += incr
         if incr <= total_rows :
            if x_dir == True :
               d_list[this_row].append( num_ctr )
            else :
               d_list[this_row].appendleft( num_ctr )

      ## increment the number and add one to row, or subtract one
      ## depending on the direction
      y_dir = not y_dir
      for k in range( 0, num_cols ) :
         num_ctr += incr
         if incr <= total_rows  :
            if y_dir == True :
               if this_row < len(d_list)-1:
                  this_row += 1
                  d_list[this_row].append( num_ctr )
            else :
               this_row -= 1
               d_list[this_row].appendleft( num_ctr )
      num_cols += 1

   ## print the list
   for p in range( 0, total_rows ) :
      for each_num in d_list[p] :
         print "%3d" % (each_num),
      print

##========================================================================
if __name__ == "__main__" :
    print_spiral( 6, 6 )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.