Thanks to Dr. Alan McLeod for the content of this lab.

This set of exercises will give you the chance to practice with loops and conditionals. Some sample answers are provided, but try the exercise on your own before looking at the sample answer. These programs are getting large enough that your version will not necessarily match the sample one, and that is OK as long as your version is just as efficient.

These exercises start simple and get more complicated. You may wish to save some of them for another time if you'd like to get started on your assignment. Make sure to complete all of these exercises later, though; they are excellent practice problems for loops.

Exercise 1: Simple Loops

1.a) Write a program that displays the numbers between 5 and 100 by fives, one number per line.

1.b) Write another program that first prompts the user for any two integer numbers whose difference is at least 6. Just exit the program if this condition is not satisfied. Then display each number that is a multiple of 5 in between the two numbers, as well as the numbers themselves. If the first number is the smallest then the numbers are all displayed in increasing order, but if the first number is larger, display the numbers in decreasing order. (Answer: Exercise1b.py)

1.c) Write a program that displays all the numbers, greater than one, that divide a number, obtained from the user, evenly (no remainder after division). If the number does not have any even divisors then print "None" instead. Do not include the number itself. (Answer: Exercise1c.py)

Exercise 2: Nested Loops

2.a) Create the "times table" for the numbers 2 to 15. Display the table with the numbers 2 to 15 across the top and down the leftmost column. Line up the results so the table looks nice! (Answer: Exercise2a.py)

2.b) In lecture we may have had time for a demo which displayed a rectangle of stars (BoxDrawing.py and BoxDrawingNestedLoops.py). Use a modified version of this program to display a triangle with the peak centred at the top of the display and the base at the bottom. You will need to build each row using the space character (" ") and the star ("*"). The number of each will depend on which row you are building. If you want to get fancy, prompt the user for the size of the triangle, in terms of the base width and the height.

Exercise 3: Loops and Conditionals

The random module (use import random) has a function called random.randint(low, hi) that returns a random integer between low and hi, inclusive. Prompt the user for the value of hi, which must be at least 10, and then generate a random number between 1 and this value. Use a loop to get the user to guess this unknown value. Keep re-prompting him until he guesses the value correctly. To make the game playable, the program should tell the user if he has guessed higher or lower than the unknown value. (Answer: Exercise3.py)

Modify the program above to play the game multiple times. You will need an outer loop and some way of stopping the game. For example, you could ask the user if he wants to play again, or let him pick a negative number for the range limit.

More Exercises

You should be able to do any of the Programming Exercises at the end of Chapter 5. I think they are all rather boring, but they are simple enough if you are having problems with the exercises given above. Problems 4, 6 and 7 don't look too bad.