Multiplication Table

Back to index

This sample program does a nice job of demonstrating the use of for loops with the range operator. It includes both a for loop and a nested (or embedded) for loop. Get this sample program up and running and make sure you understand how it works.
#!/usr/bin/python # simple maze using 5x5 grid from tkinter import * import math master = Tk() w = Canvas(master, width=240, height=240) w.pack() w.create_rectangle(0,0,240,240,fill="yellow") for i in range(0,6): w.create_rectangle(1+40*(i%6),1+40*math.floor(i/6),40*(i%6)+38,40*math.floor(i/6)+38,fill="black") w.create_rectangle(1+40*math.floor(i/6),1+40*(i%6),40*math.floor(i/6)+38,40*(i%6)+38,fill="black") if i==0: w.create_text(20,20,text="X", fill="white", font=('Helvetica',28)) else: w.create_text(20+i*40,20,text=i, fill="white", font=('Helvetica',28)) w.create_text(20,20+i*40,text=i, fill="white", font=('Helvetica',28)) for x in range(1,6): for y in range(1,6): w.create_rectangle(1+40*x,1+40*y,40*x+38,40*y+38,fill="#000099") w.create_text(20+x*40,20+y*40,text=x*y,fill="white", font=('Courier',20)) mainloop()
Although the arithmetic in this sample is simple, there's a lot of it and so you may need to concentrate on a line at a time when analysing the program. You might even want to make copies of your program file and delete most of the lines so you can see the behavior of a smaller subset of lines. Another approach is to change the color designation in a create_rectangle or create_text line so you can isolate its behavior. In any case, it helps to be systematic in your investigation and also you may want to consult an online resource.
ASSIGNMENT:

Make the following changes to the sample program:

  1. Expand your table to cover integers up to twelve
  2. Reduce the size of cells from 40x40 to 30x30
  3. Change the size of the canvas area to fit the table you produce
  4. Change the font sizes to fit the cells