The first example from this week was a count down. This would display on screen a count down from 10 to 0.
import sys #This will give us some control over the time import time # We want the text around the center xCenter = 400 yCenter = 300 # Set up my font, with a size of 100 pixels print "font arial 100 BOLD" # My looping conditions start = 10 end = -1 inc = -1 #Wait a second time.sleep(1) #For each number we want to for i in range(start,end,inc): #Wait one second time.sleep(1) print "color 0 0 0" #Clear the screen print "clear" print "color 255 255 255" #Then write the number print "text",i, xCenter, yCenter sys.stdout.flush()
The important thing from this example was
#Our looping Conditions start = #what value do we want to start with# end = #at what value do we want to stop# inc = #how do we want our values to change from one to the next# for i in range(start,end,inc): #Do something` # # #
In many ways, this behaves like the intersection example from last week. If our start were 10, and our end were 0, and our inc was -1, we would go down one number each time from 10 to 1 (remember, we won’t do anything with 0, cause we stop)
The second example this week was a mathematical table. In this example we will have the numbers from 0 to 15 as a header row, and column. We will then will in the position at the row and column with the multiplication of the row and column header.
#Where do I want my table to start xStart = 100 yStart = 100 #How much space do I want between numbers spacing = 30 #How do I want my font set up print "font arial 10 BOLD" #Loop variables start = 0 end = 16 #Draw lines to separate the numbers we want to multiply from the answers print "line",xStart - spacing / 2,yStart - spacing,xStart - spacing/2,yStart + spacing * end print "line",xStart - spacing,yStart - spacing/2,xStart + spacing * end,yStart - spacing / 2 #Write the numbers on the top and side for i in range(start,end): print "text",i,xStart + i*spacing, yStart - spacing print "text",i,xStart - spacing, yStart + i*spacing #Fill in the columns and rows. In this example, with the number at the top multiplied by the number on the side for i in range(start,end): for j in range(start,end): # Multiply I and J, and put the answer at x column i and y row j print "text",i*j, xStart + i*spacing, yStart + j*spacing
In this example we use a nested for loop
#loop conditions start = #where to begin# end = #where to end# inc = #how to change from one to the next# for i in range(start,end): for j in range(start,end): #Do something with a combination of i and j` # # # # #