Debugging challenges

Python Minecraft - Lesson 2

Objectives

  • To set up a Raspberry Pi computer with some assistance
  • To begin to use a text based programming language
  • To use logical reasoning to debug errors in a program

Lesson Resources

 

Lesson 1 - Hack the chat

Lesson 2 - Debugging challenges

Lesson 3 - Using 3D coordinates

Lesson 4 - Setting Blocks

Lesson 5 - Setting Block Challenges

Lesson 6 - Going loopy

Introduction

In our last lesson we learned how to set up our screen so we could write code with Python and see the effect it had on the Minecraft world side by side.  We learned how to display messages in the game’s chat pane, time when the messages appear, and even how to randomise which message (from a list) is displayed to the player. 

Ask the students to set up their Raspberry Pi as we did in the first lesson. Once set up, they should open and arrange the three windows as we did in the last lesson. 

 

 

Challenge 3: User Generated Messages in the Chat Pane!

The previous examples held the statements within the program, but it might be cool for us to add statements when we want them, then we could make it say what we want, when we want it. To do this we will need to use the raw_input command. This will allow us to type our statements into the Python Shell and they can then be displayed in the Minecraft console. Take a look at the code below:

 

 

When you run this program something different will happen. In our previous examples we have told the computer what to say by typing our statements directly into the code. This program is a little different as it requires a user input. 

When this program runs the Minecraft screen will not display a message UNTIL the user types something. But where do you type the message? In the Python shell of course! (the bottom right window on your screen).

When the code runs, a message will display in the shell saying What should we say?.  You can see the code for this on line 7 above.  When the user presses the enter key, the text they have “input” is stored as a variable called “text”. The computer then waits for 5 seconds and posts their message (“text”)  to the chat pane. This process continues until the program is quit. When you run this program you should see the following:

 

 

Notice the blue text in the Python Shell under the Minecraft screen. This is where the user will type the message.

When we type a message into the Python Shell it will display in the Minecraft chat pane 5 seconds later, like this...

 

 

The message next to the blue writing in the shell is now displayed in the Minecraft screen.

 

So… Why is this important?

Throughout this lesson we have been hacking the chat pane to display different messages. We have looked at and developed quite a lot of skills, but you may not have realised that you have actually created something  that is used a lot in our everyday lives. 

Many social media sites have associated “Messenger” apps: Google Chat, Facebook Messenger, WhatsApp, Twitter and the rest do the exact same thing you just created. 

They take a user input, save it as a variable and then post it to a specified place. 

The only difference is that rather than posting to the Minecraft window, the applications listed above post the message to web pages. The idea is exactly the same though. 

So well done! You just created your own Minecraft Messenger!! 

Next, have a look at this debugging exercise, 

 

Syntax Spot the difference:

During our coding we may have made mistakes. That’s okay, getting stuff wrong is all part of the learning process. The mistakes you will have made are known as “SYNTAX ERRORS”, and the following exercise is aimed at getting you to think about these. 

 

What is Syntax?

Syntax is the grammar, structure, or order of the elements in a language statement. Syntax applies to computer languages as well as languages like English, French, Spanish etc (other languages are available). Usually, we think of syntax as "word order." In computer languages, syntax can be extremely rigid as in the case of most assembler languages or less rigid in languages that make use of "keyword" parameters that can be stated in any order.

 

Below you can see an image of the correct code from Hack 1. The other images all have syntax mistakes in them. Can you spot the differences? Each image has 1 difference from the original file. All these images are in the teacher presentation, side by side with the correct code each time. Display each one for the students to study. Ask them to spot the error. After each one, reveal the bug and how the error message that would be displayed in the Python shell if they ran the code. Point out how this message directs us to our error and should be used to help us with debugging our code when needed. 

 

Original File

 

Syntax Spot 1

 

Syntax Spot 2

 

Syntax Spot 3

 

Now you have spotted the differences, can you note what line the mistake is on, and what part of the line is incorrect?

The error messages in the Python shell help to direct us to the error so we can fix it. 

 

Syntax Spot 1

 

Syntax Spot 2

 

Syntax Spot 3

 

 

Create, spot and debug errors

Now ask the students to write out this correct code from the start of lesson 1 in their Python window. 

They should then:

  • add ONE error to their code 
  • swap computers with another pair and attempt to debug each other’s code. 
  • They should attempt to run the code so an error message is generated in the Python shell window. Encourage them to try and use this to find the error. 
  • If time, they can repeat this process with another pair.

 

Plenary

Review the lesson by asking the class:

  • What errors did you find?
  • Who used the error messages in the Python shell?
  • Were you able to correct the errors?

Be sure to leave time to disconnect the Raspberry Pi, monitor, mouse and keyboard and put them back into any PCs they were borrowed from. 

< Previous Lesson

Next Lesson >