Program Arcade Games
With Python And Pygame

Language
English
Russian - Русский
Turkish - Türkçe

Lab 4: Loopy Lab

4.1 Part 1

Write a Python program named part_1.py that will print the following:

10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54

Tips for Part 1

This problem requires a bit of an “a-ha” to get. Make sure to ask around if you have problems. My students often find it to be one of the harder problems in this course.

4.2 Part 2

Write a program named part_2.py that:

  1. Takes a list of five numbers from the user
  2. Prints the list
  3. Prints the average
  4. Modifies the list so each element is one greater than it was before
  5. Prints the modified list

Example:

Part 2
Enter 5 numbers:
Enter a number: 4
Enter a number: 8
Enter a number: 2
Enter a number: 3
Enter a number: 5
You entered: [4, 8, 2, 3, 5]
The average of those numbers is: 4.4
That same list with 1 added to each number: [5, 9, 3, 4, 6]

Tips for Part 2

This part demonstrates how to create a list, how to total a list, and how to change each item in a list.

4.3 Part 3

Start with the pygame template code:
pygame_base_template.py

Rename the file to part_3.py. Use nested for loops to draw small green rectangles. Make the image look like Figure 26.1.

fig.grid
Figure 26.1: Pygame Grid

Do not create the grid by drawing lines, use a grid created by rectangles.

If this is too boring, create a similar grid of something else. It is ok to change the color, size, and type of shape drawn. Just get used to using nested for loops to generate a grid.


You are not logged in. Log in here and track your progress.