Level 2

icon picker
Challenge 4


Become profficient at debugging.
Become more familiar with scope

Task 1: Scope Definitions

Learning Objectives: PY08, PY08.1
For the final Challenge in Level 2, we’ll be doing some research around scope.
Please write these in a text editor, as we will ask you to send us these in the check in email after the task.
Write a max 100 word definition of the term ‘global scope'.
Write a max 100 word definition of the term ‘local scope’

Task 2: Debugging

Learning Objectives: D01, D02, D03, D03.1
Read all the introduction to the problem;
Change the given code to get the correct output.
animals = [ { "name": "Fluffy", "type": "dog" }, { "name": "Parsley", "type": "dog" }, { "name": "Ginger", "type": "cat" }, { "name": "Biscuit", "type": "cat" } ]
def say_hello_to_pets(pets): for pet in pets: hello_message = ""; if pet["type"] == "dog": hello_message = "Woof" pet_name = pet.name if pet["type"] == "cat": hello_message = "Meow" pet_name = pet.name print(f"{hello_message}, {pet_name}!"); if __name__ == "__main__": say_hello_to_pets(animals)
I want to say hello to all of the pets in my array of dictionaries. Currently, this code doesn’t work - it is up to you to change the code to return the correct output. When running it, you should see the below error in the output.
Screenshot 2023-03-30 at 12.06.10.png
Running this code will lead to an AttributeError - when debugging it is essential to read the message to get clues as to what is wrong.
Documentation can guide you on why that error has been flagged.
💡 can be found here.
The second part of the message specifies what is wrong - in this case, the log tells us that the dict has no attribute name.
Run through your inputs, outputs and which steps should be happening when.
Here are some questions to consider:
What steps should be taken to achieve the output?
Which order should the steps happen in?
Which steps are currently running - are there any missing?
Which order is my code currently executing in?
Tracing back your code step by step can help you debug errors, but printing out the values throughout your code using print() can give you a better view of how values change.
Remember that this code was written to be wrong - feel free to change the code to suit best your way of working. Good code should be easy to read and as simple as possible. We would encourage you to write more test cases as this is an excellent habit to get into.
The expected output can be found below:


Continue to to submit your work for this Level
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.