Grids
Q1. Type in this code and get it working:
1 for x in range(1,4):
2 for y in range (1, 4):
3 print("\t",x*y,end=" ")
4
5 print("\n")
Q2. Describe how this code works and what it produces.
Q3. Dry-run the code using the following table, which has been started for you. Remember, you should only enter a value when it changes.
Q4. What does the \t do in line 3?
print("\t",x*y,end=" ")
Q5. What does the end=" " do in line 3?
print("\t",x*y,end=" ")
Q6. Your task is to print out the following grid of numbers:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
The pseudo-code is given below:
set a counter to zero
for numbers =1 to 25
print number
counter = counter + 1
if the counter mod 5 equals 0
go on to the next line
In this line:
if the counter mod 5 equals 0
what you are doing is seeing if you have printed off 5 numbers in a row. If you divide your counter by 5 and the remainder is zero, then you must have printed off 5 numbers and must therefore move to he next line. Mod is used to calculate the remainder.
Extension
Try printing off bigger grids, perhaps 10 by 10, or create grids using characters rather than numbers.