Debugging Strategies

Time: 10-15 minutes

Purpose: Give you practice with debugging errors in python.

Introduction: Debugging is an important skill to have in programming. The tasks below will help you gain skills in debugging. Don’t forget to use the strategies outlined in the video to help you figure this out. If you get stuck and can’t work something out post on here and we will try to help you out with it!

Tasks:

  1. there are 4 errors in the code below. Copy the code into a file and run it and see if you can fix them all. You can download the file from github here if you prefer
my_list = [1,2,3]

three = my_list[3]

if count == True:

my_list.length()
  1. There are 4 errors in the method below.Copy the code below into a script and run it to see if you can figure out how to fix them. A file with this code in it can also be downloaded from github here
def cool_function(temperature):
    #temperature in celcius
    if temperature > 28:
        print ("Turn on the air. It's "+temperature+ " degrees in here!")
    elif temperature = 28:
        print ("Ahh just right!")
    else
        print ("Brr! It's freezing in here. Someone please turn up the heat.)

cool_function(25)
cool_function(28)
cool_function(30)
  1. Copy the code below in to script and debug it so that is prints out “Hello Mrs. Janet Jackson!”
    A file with this code in it can also be downloaded from github here
def printWelcomeString(name = '', greeting = '', title = ''):
    '''prints out a personalized welcome that includes
    the user's full name and a greeting message. 
    
    There is a logic error in here. You may want to add print statements
    to help figure out what is going on'''
    
    #name and greeting are optional.
    #Set defaults in case the caller doesn't enter them 
    if not greeting:
        greeting = 'Hi'
    else:
        name = 'Anonymous'
    welcome_string = '%s %s %s!'%(greeting, title, name)

    print(welcome_string)

#should print out "Hello Mrs. Janet Jackson!", but it does not. 
printWelcomeString(name = 'Janet Jackson', greeting = 'Hello', title = 'Mrs.')