CSCI 171: Lab exercise 1


Student name:


In this lab you will work through a set of problems that form the basis for the first lab exercise, worth 5% of your total course mark, and to be submitted by the start of your lab session on Tuesday February 16th.

The initial setup involves copying four pairs of programs from the instructor's account to your own, and making each of the programs executable.

Once that is complete, you will be running each pair of programs, recording the results in the space below, and then writing a short analysis explaining the behaviour of the different programs. (All the details are provided below.)

Turn in the (paper) copy of your work by the deadline noted above.

Initial setup

First, as always, move into your csci171 directory, create a directory for this week's lab, and move into that. E.g.

   cd csci171
   mkdir lab4
   cd lab4
Next, copy across some files for this week's lab (from Dave's account to your current directory):

   cp  ~wesselsd/labfiles/lab4/*  .
This copies ten files to your folder:
bck_ocean.gif     star.gif
part1a.py         part1b.py
part2a.py         part2b.py
part3a.py         part3b.py
part4a.py         part4b.py
Run the ls command and check to see that all ten files are present in your current directory.

Run the chmod command to make the programs executable:
chmod u+x *.py

This completes the setup, you can now proceed to the four key parts of the lab below


Part 1: while loops

The two programs part1a.py and part1b.py are identical except for the indentation of one line of code.

Your objective for this part of the exercise is to run the two programs, write the output each produces in the table below, and then explain why they differ the way they do based on the code and the output.

 


part1a.py part1b.py
#! /usr/bin/python

lives = 3
while lives > 0:
   lives = lives - 1
   print "lives left: ", lives 
#! /usr/bin/python

lives = 3
while lives > 0:
   lives = lives - 1
print "lives left: ", lives 
Output from part1a.py












Output from part1b.py












Explanation of the difference in output between 1a and 1b












Part 2: if statements

The two programs part2a.py and part2b.py are identical except for the indentation of one line of code.

Your objective for this part of the exercise is to run the two programs, write the output each produces in the table below, and then explain why they differ the way they do based on the code and the output.

part2a.py part2b.py
#! /usr/bin/python
lives = 3
if lives > 5:
   print "more than 5 lives"
   if lives < 20:
      print "less than 20 lives"
else:
   print "5 or fewer lives" 
#! /usr/bin/python
lives = 3
if lives > 5:
   print "more than 5 lives"
if lives < 20:
      print "less than 20 lives"
else:
   print "5 or fewer lives" 
Output from part2a.py








Output from part2b.py








Explanation of the difference in output between 2a and 2b















Part 3: if/elif/else statements

The two programs part3a.py and part3b.py are identical except for the use of elif instead of if in one line of code.

Your objective for this part of the exercise is to run the two programs, write the output each produces in the table below, and then explain why they differ the way they do based on the code and the output.

(Hint: elif is short for "else if>".)

part3a.py part3b.py
#! /usr/bin/python
lives = 3
if lives < 5:
   print "less than 5 lives"
if lives < 20:
   print "less than 20 lives" 
#! /usr/bin/python
lives = 3
if lives < 5:
   print "less than 5 lives"
elif lives < 20:
   print "less than 20 lives" 
Output from part3a.py








Output from part3b.py








Explanation of the difference in output between 3a and 3b















Part 4: for loops and events

Programs part4a.py and part4b.py are identical except for how they respond when the user presses the arrow keys.

Your objective for this part of the exercise is to run the two programs and describe the difference in behaviour, then study the code snippets in the table below to explain why they respond the way they do.

part4a.py snipped part4b.py snipped
if event.key == pygame.K_RIGHT:
   starSpeed[0] = starSpeed[0] + basicSpeed
   print "Player hit right arrow, new speed: ", starSpeed
elif event.key == pygame.K_LEFT:
   starSpeed[0] = starSpeed[0] - basicSpeed
   print "Player hit left arrow, new speed: ", starSpeed
elif event.key == pygame.K_UP:
   starSpeed[1] = starSpeed[1] - basicSpeed
   print "Player hit up arrow, new speed: ", starSpeed
elif event.key == pygame.K_DOWN:
   starSpeed[1] = starSpeed[1] + basicSpeed
   print "Player hit down arrow, new speed: ", starSpeed 
if event.key == pygame.K_RIGHT:
   starSpeed[0] = basicSpeed
   print "Player hit right arrow, new speed: ", starSpeed
elif event.key == pygame.K_LEFT:
   starSpeed[0] = -basicSpeed
   print "Player hit left arrow, new speed: ", starSpeed
elif event.key == pygame.K_UP:
   starSpeed[1] = -basicSpeed
   print "Player hit up arrow, new speed: ", starSpeed
elif event.key == pygame.K_DOWN:
   starSpeed[1] = basicSpeed
   print "Player hit down arrow, new speed: ", starSpeed 
Description of how the behaviour differs between 4a and 4b
















Explanation of the difference in behaviour between 4a and 4b