CSCI 171: Lab exercise 1 sample solution


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
lives left: 2
lives left: 1
lives left: 0
Output from part1b.py
lives left: 0
Explanation of the difference in output between 1a and 1b
In both cases, the while loop repeats its sequence of actions three times,
once when lives is three, once when lives is two, once when lives is one.

Remember that, in python, indentation determines which part of a
   program are "inside" one another.  In the programs above, the
   two while loops use indentation to show which sets of actions
   get repeated every time the while loop runs.

In part a, the while loop actions consist of subtracting one from
   lives and then printing the number of lives remaining, hence the
   three "lives left" statements.
   
In part b, only the subtraction part is 'inside' the while loop,
   and the print statement comes AFTER the while loop ends.  Hence
   a single "lives left" result gets printed, just before the program
   ends.

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
5 or fewer lives
Output from part2b.py
less than 20 lives
Explanation of the difference in output between 2a and 2b

As with the while loops from part 1, indentation determines
which sets of actions are grouped or contained in one another.

In part a, there are two main actions, performed in order:
   (1) give lives the value 3
   (2) the primary if/else statement, which says
       if the number of lives is greater than five
          then do these two things:
	      first, print the message "more than 5 lives"
	      second, run another check to see if the number 
	          of lives is less than 20 (implying the number of
		  lives must be between 6 and 19, since we already
		  know the number of lives is greater than 5)
       otherwise (i.e. if the number of lives is not greater than five) 
          then do this one thing:
	      print the message "5 or fewer lives"

In part b, on the other hand, there are three main actions:
   (1) give lives the value 3
   (2) check to see if the number of lives is greater than 5,
          and print "more than 5 lives" if so
   (3) check to see if the number of lives is less than 20
          (regardless of which way part 2 got answered)
       print "less than 20 lives" if so,
          otherwise print 5 or fewer lives.

OBSERVATION: if the number of lives was more than 20 part b 
    would give incorrect information:
        it would say "more than 5 lives" because of step (2),
        but it would also erroneously say "5 or fewer lives" because
	in step (3) it would run the "else" case

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
less than 5 lives
less than 20 lives
Output from part3b.py
less than 5 lives
Explanation of the difference in output between 3a and 3b
Part a consists of three statements run independently:
   (1) set the number of lives at 3
   (2) if lives is less than 5 
          then print a message saying so
   (3) if lives is less than 20 
          then print a message saying so
Note that step 3 is checked regardless of what
   happens in step 2

Part b consists of two statements:
   (1) set the number of lives at 3
   (2) if lives is less than 5 
          then print a message saying so,
       otherwise (i.e. if the number of lives is not less than 5)
          if lives is less than 20 
	     then print a message saying so
	     (this only happens if lives is in the range 5-19)

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
In part a, the more often you press a specific arrow key
   the faster you move in that direction.

In part b, on the other hand, you have a fixed speed of travel,
   and the arrow keys just determine whether you are moving
   left vs right, and up vs down.

Explanation of the difference in behaviour between 4a and 4b
In part a, the behaviour is controlled by adding or subtracting
   a fixed amount to your horizontal and vertical speed.

   For example, suppose basicSpeed is 4, and you are already moving
       to the left.  Each time you hit the left arrow key, your 
       leftward speed increases by 4, whereas each time you hit the
       right arrow key your leftward speed decreases by 4 (until 
       eventually you would stop moving to the left and would start
       moving right).

In part b, on the other hand, hitting the left arrow sets your leftward
   speed to basicSpeed (e.g. sets your horizontal speed to 4 no
   matter what it was before), hitting the up arrow sets your vertical
   speed to -4 no matter what it was before, etc.