Difference between revisions of "Debugging short exercise"

From WLCS
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
##Inside of the ''if'' statement that checks if the ball has been caught  
 
##Inside of the ''if'' statement that checks if the ball has been caught  
 
##End of program  
 
##End of program  
# Run the program and stop it ''as soon as the buggy behavior has occurred''.  
+
# Print the distance before the 'if' statement (so that it runs on every iteration): <syntaxhighlight lang="python"> print "Distance/ball to mitt: ", round(distance(ball_x, ball_y, mitt_x, mitt_y)) </syntaxhighlight> <br/> '
# Analyse the console output (the debug 'trace'). <br /> What would you expect to see if the program was running properly?
+
# Run the program and stop it ''as soon as you notice the buggy behavior''.  
#Is the problem that the distance isn't being properly calculated? <br /> Add another print statement that prints it,  like this: <syntaxhighlight lang="python"> print "Distance: "+ distance(ball_x, ball_y, mitt_x, mitt_y) </syntaxhighlight>
+
# Analyse the console output (the debug 'trace'). <br /> '''Is the execution sequence what we would expect?''' (e.g. is it getting to the right place in the code at the right time?) <br />   '''Are the values what we would expect?'''
# Once you've worked out what the real problem is, fix it.
+
# Once you've worked out what the problem is, fix it.
# Submit your code to school web locker
+
# ''Name your file "MY_NAME_DEBUG_EX.txt"'' and then submit your code to school web lockers.
 +
# GOOD LUCK!
  
 
<syntaxhighlight lang="python">   
 
<syntaxhighlight lang="python">   
Line 21: Line 22:
  
 
def distance(x1, y1, x2, y2):
 
def distance(x1, y1, x2, y2):
     return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
+
     return ((x2 + x1)**2 + (y2 + y1)**2)**0.5
  
 
begin_graphics(800, 600, title="Catch", background=color.YELLOW)
 
begin_graphics(800, 600, title="Catch", background=color.YELLOW)
Line 29: Line 30:
 
ball_y = 300
 
ball_y = 300
 
ball = Circle((ball_x, ball_y), 10, filled=True)
 
ball = Circle((ball_x, ball_y), 10, filled=True)
 +
 
dx = 4
 
dx = 4
 
dy = random_between(-4, 4)
 
dy = random_between(-4, 4)
Line 34: Line 36:
 
mitt_x = 740
 
mitt_x = 740
 
mitt_y = 300
 
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 60)
+
mitt = Circle((mitt_x, mitt_y), 30)
  
 
while True:
 
while True:
Line 45: Line 47:
 
         dx *= -1
 
         dx *= -1
 
      
 
      
    ball_y += dy   
 
    ball_x += dx
 
    move_to(ball, (ball_x, ball_y))
 
 
 
     # check on the mitt
 
     # check on the mitt
 
     if key_pressed('k') and mitt_y <= 580:
 
     if key_pressed('k') and mitt_y <= 580:
Line 54: Line 52:
 
     elif key_pressed('j') and mitt_y >= 20:
 
     elif key_pressed('j') and mitt_y >= 20:
 
         mitt_y -= 5
 
         mitt_y -= 5
 +
 +
    ball_y += dy   
 +
    ball_x += dx
 +
    move_to(ball, (ball_x, ball_y))
  
 
     if key_pressed('escape'):
 
     if key_pressed('escape'):
Line 60: Line 62:
 
     move_to(mitt, (mitt_x, mitt_y))
 
     move_to(mitt, (mitt_x, mitt_y))
  
     if distance(ball_x, ball_y, mitt_x, mitt_y) <= -60:  # ball is caught
+
     if distance(ball_x, ball_y, mitt_x, mitt_y) <= 30:  # ball is caught
 
         remove_from_screen(ball)
 
         remove_from_screen(ball)
 
         break
 
         break

Latest revision as of 23:03, 3 February 2011

Objective: Practice debugging techniques

This program is supposed to break once the ball has been caught, but it doesn't work. Your job is to work out why, and then fix it.

  1. Copy the program at the bottom of this page, and open it in Dr. Python
  2. Add print statements to trace program execution, like this:
     print "Trace: At start"
    
    Add at least five statements:
    1. Start of the program
    2. Once the game window has been created
    3. Inside of the main game update loop
    4. Inside of the if statement that checks if the ball has been caught
    5. End of program
  3. Print the distance before the 'if' statement (so that it runs on every iteration):
     print "Distance/ball to mitt: ", round(distance(ball_x, ball_y, mitt_x, mitt_y))
    

    '
  4. Run the program and stop it as soon as you notice the buggy behavior.
  5. Analyse the console output (the debug 'trace').
    Is the execution sequence what we would expect? (e.g. is it getting to the right place in the code at the right time?)
    Are the values what we would expect?
  6. Once you've worked out what the problem is, fix it.
  7. Name your file "MY_NAME_DEBUG_EX.txt" and then submit your code to school web lockers.
  8. GOOD LUCK!
   
from gasp import *

def distance(x1, y1, x2, y2):
    return ((x2 + x1)**2 + (y2 + y1)**2)**0.5

begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)

ball_x = 10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)

dx = 4
dy = random_between(-4, 4)

mitt_x = 740
mitt_y = 300
mitt = Circle((mitt_x, mitt_y), 30)

while True:
    # move the ball
    if ball_y >= 590 or ball_y <= 10:
        dy *= -1
        
    ball_x += dx
    if ball_x >= 810 or ball_x <= 10:
        dx *= -1
    
    # check on the mitt
    if key_pressed('k') and mitt_y <= 580:
        mitt_y += 5
    elif key_pressed('j') and mitt_y >= 20:
        mitt_y -= 5

    ball_y += dy    
    ball_x += dx
    move_to(ball, (ball_x, ball_y))

    if key_pressed('escape'):
        break

    move_to(mitt, (mitt_x, mitt_y))

    if distance(ball_x, ball_y, mitt_x, mitt_y) <= 30:  # ball is caught
        remove_from_screen(ball)
        break

    update_when('next_tick')

end_graphics()