Rubber Ducky Debugging#

If you are having troubles finding out where an error is in your code, you can also turn to ‘Rubber Ducky Debugging’. The idea of this is that it forces you to explain, in very simple language and step-by-step, what you want your code to do and what each line of code does. Your rubber ducky does not need to study engineering, they have a very simple life, so they need a very gentle introduction to your code. Often, you will find an error in the code logic while talking through it. This is not to say that your rubber ducky is not smart, they are simply smart in other ways.

You can read more about Rubber Ducky Debugging here: https://rubberduckdebugging.com/

duck

Task 4.1 Get a rubber duck#

You can have your choice of duck. If you are not able to obtain a duck, a goose (not angry) will also do the trick.

Task 4.2 Name your rubber duck#

We especially encourage pun names. This is important to build a rapport with your duck. Tip: calling your duck goose or your goose duck is always hilarious.

Task 4.3 Do some debugging!#

Below are some short snippets of code. See if you can find out what the bugs are, with your new friend.

You do not need to submit anything for this task

# This function should calculate the average, given a list of numbers

def average(numbers):
    total = 0
    for n in numbers:
        total = total + n
    avg = total / len(numbers) -1
    return avg

print(average([3, 5, 7]))
4.0

Solution:

  • We create a variable total to store the maximum value and set it to 0

  • We go through every number in the list of numbers:

    • The total gets updated with the current number

    • Divide the current total by the number of numbers but we should not subtract by 1!

avg = total / len(numbers) -1 should be changed to avg = total / len(numbers). It will return 5.

# This function should find the highest value in a list of numbers

def find_max(numbers):
    max_val = 0 
    for n in numbers:
        if n > max_val:
            max_val = n
    return max_val

print(find_max([-3, -5, -1]))
0

Solution:

  • We create a variable max_val to store the maximum value and set it to 0

  • We go through every number in the list of numbers:

    • If the current number us higher than the value of max_val, replace it.

    • Otherwise, keep it as is.

  • There is no condition for negative numbers

Instead of picking a value to initialise max_val at, we can tell it it initialise at the first entry of the list of numbers. This way we know that only numbers in the given list is evaluated.max_val = 0 should be changed to max_val = numbers[0]. It will return -1.

# This function should sum all the even values in a list of numbers

def sum_evens(numbers):
    total = 0
    for n in numbers:
        if n % 2 == 0:
            total += n
        else:
            total = 0 
    return total

print(sum_evens([1, 2, 4, 5, 8]))
8

Solution:

  • We create a variable total to store the current sum and set it to 0

  • We go through every number in the list of numbers:

    • If the current number is divisible by 2, add this to total

    • If not, the total is set to 0 this resets the total, irrespective if the value has been updated previously!

Instead of resetting total each time an odd number is encountered, we should simply skip the number. We can do this by simply removing the else statement completely. It will return 14.