New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

# Live coding

Live coding is a great tool and requires a little bravery.
2.6
Live coding is a great tool to demonstrate how you code and solve a problem. It demonstrates good practise and models your thought processes in front of your learners. Live coding is most effective when it is carefully planned to cover the required concepts, consolidate learning or address misconceptions. Let’s explore a live coding activity to create a snack generator with your students. We’ll assume that they already know how to find a random integer, but don’t know how to use lists. The snack generator will have a list to hold all the different snacks in one structure. A list looks a little like a variable at first, but square brackets and commas separate list items.
43.9
Here is a list that I’ve done already in Python, and it’s got my snacks already stored in there. So you’ve got the name for the list, which is snack, and then inside it, we’ve got apple, banana and raspberries. And we’ve also got this bit below that just says print snacks. So have a think about what might happen when I press run on this one. Hopefully you got it right, it’s just going to print the entire list on the screen. But that’s not going to help us with our random snack generator. We want it to just print one item. So what we need to do now is get a random number to print that random item from the list.
82.4
So I’m going to make a variable, I’m going to call it random number and I’m going to get some user input.
92.3
And that input is then going to be put into our print statement. So we go to print, snack. And now we use the square bracket to put in our variable, which is holding the random number. Now, I’ve made a deliberate mistake here. So I wonder if you can spot it. Run it, and I’m going to type in a number, and we’ve got an error. So if we take a look right here, it’s mentioning integers, not string. And it’s because, at the moment, that input is being seen as a string and it needed to be an integer. And, hopefully, your learners will spot that too. And we just need our int in front of there and our brackets.
138.6
And then, hopefully, next time, it’s going to work. So I’m going to type in a 1 now. And have a think about– hoping that it’s worked this time, there’s no error– what do you think is going to appear on the screen? Ah, it’s done banana. Now, if you thought Apple then you’re probably the same way as most other learners as well. They think that, well, that must be location 1 because it’s at the beginning. But, actually, in Python, the lists start at zero. So in order to get apple to appear, I’m going to have to type in a zero at that point, and then I get to my apple. So it’s another good thing to just point that out there.
178.6
And you can try the different items too so that you can see them all working. But at the moment, our code, again, not very exciting, because I’m just typing in a number and just displaying it on the screen, but we want it to be random. So we’ve already got a bit of code that we know about already called randint. And we can just change that input to randint instead. And we know that the locations start at 0, and if we count along, we’ve got 0, 1, 2. So our range is going to be 0 to 2. And this time, I’m going to do another deliberate mistake for you. And hopefully, your learners can pick this one up.
218.3
You’ve got another error message. So down on the bottom it’s saying, randint is not defined. And that’s because we have an imported random integer from the top. So I can just add that little bit of code in. Nothing wrong with making mistakes in front of your learners. So from random import randint. And then, hopefully, fingers crossed it’s all going to work properly this time. And it has. Its randomly shown me apple. And every time I run it, it might not come up a different one. It might repeat the same one again. But it is randomly selecting now those different items from that list.
263
Following this example, your learner should have a grasp of how to create a list, how to call one item in the list and how to pick an item at random. This is all you need to get started with a snack generator. Your students can then adapt and improve the code. When live coding, remember to talk to your learners and ask them questions, narrate your inner monologue and slow down to give your learners time to process. Make mistakes and show your learners how code moves around. It isn’t just written perfectly from top to bottom. Make sure you’re visible. Let your students see your face, and don’t turn your back for too long.
302.3
Use the largest font possible without losing view of the full line of code. And be sure to break up your code into smaller chunks. Pause to write things on the board, draw diagrams and work things out. Have you used live coding in the classroom before? Discuss your experiences and share your top tips with your fellow learners in the comments section.
361.6
The snack generator will have a list to hold all the different snacks in one structure. A list looks a little like a variable at first, but square brackets and commas separate list items. Here is a list that I’ve done already in Python. And it’s got my snacks already stored in there. So you’ve got the name for the list, which is snack, and then inside it, we’ve got apple, banana and raspberries. And we’ve also got this bit below that just says print snacks. So have a think about what might happen when I press run on this one. Hopefully, you got it right. It’s just going to print the entire list on the screen.
398.8
But that’s not going to help us with our random snack generator. We want it to just print one item. So what we need to do now is get a random number to print that random item from the list. So I’m going to make a variable, I’m going to call it random number and I’m going to get some user input.
421.1
And that input is then going to be put into a print statement. So go to print, snack. And now, we use the square bracket to put in our variable, which is holding the random number. Now, I’ve made a deliberate mistake here. So I wonder if you can spot it. So run it, and I’m going to type in a number, and we’ve got an error. So if we take a look at it here, it’s mentioning integers, not string. And it’s because, at the moment, that input is being seen as a string and it needs it to be an integer. And hopefully, your learners will spot that too. And we just need our int in front of there, and our brackets.
467.4
And then, hopefully, next time, it’s going to work. So I’m going to type in a 1 now. And have a think about hoping that it’s worked this time. There’s no error. What do you think is going to appear on the screen?
493.4
The snack generator will have a list to hold all the different snacks in one structure. A list looks a little like a variable at first, but square brackets and commas separate list items. Here’s a list that I’ve done already in Python, and it’s got my snacks already stored in there. So you’ve got the name for the list, which is snack, and then inside it, we’ve got apple, banana and raspberries. And we’ve also got this bit below that just says print snack. So have a think about what might happen when I press run on this one. Hopefully you got it right. It’s just going to print the entire list on the screen.
530.5
But that’s not going to help us with our random snack generator. We want it to just print one item. So what we need to do now is get a random number to print that random item from the list. So I’m going to make a variable. I’m going to call it random number. And I’m going to get some user input.
552.8
And that input is then going to be put into our print statement. So I go to print, snack. And now, we use the square bracket to put in our variable, which is holding the random number. Now I’ve made a deliberate mistake here. So I wonder if you can spot it. So run it, and I’m going to type in a number, and we’ve got an error. So if we take a look at it here, it’s, mentioning integers, not string. And it’s because, at the moment, that input is being seen as a string, and it needed to be an integer. And, hopefully, your learners will spot that too. And we just need our int in front there and our brackets.
599.2
And then, hopefully, next time, it’s going to work. So I’m going to type in a 1 now. And have a think about– hoping that it’s worked this time, there’s no error– what do you think is going to appear on the screen?

Live coding is a great tool and requires a little bravery. It is when you solve a problem in front of your learners and model your thought processes. It is key that your learners see how you code, and not just the final product.

Live coding links really nicely with worked examples and follows similar principles. You might be tempted to use live coding on an ad hoc basis in your lessons, but it is much more beneficial when it is carefully planned to cover the required concepts, consolidate learning, or address common misconceptions.

This 2018 paper states that live coding:

• Makes the process of learning programming easy to understand for novices
• Helps students learn the process of debugging
• Exposes students to good practices

### Good practices

You should:

• Make mistakes, either planned or by accident
• Slow down to give your learners time to process
• Show your learners how code moves around; it isn’t just written perfectly from top to bottom
• Be visible; let them see your face; don’t turn your back for too long
• Pause to write things on the board, draw diagrams, and work things out
• Use the largest font possible without losing view of the full line of code
• Break the code down into small chunks (decompose)

### Live coding to make a compliments machine

Here is an example live-coding activity that you could try with your learners. This example assumes that they will already know how to find a random integer, but don’t know how to use lists. This coding example allows learners to develop their understanding of computational thinking while introducing a new programming construct.

#### Scenario

The compliments machine will have a list that stores a variety of compliments. The compliments will then be chosen at random for the user.

#### Introducing lists

Lists allows us to hold groups of data in one structure. We can create a list to store all of the compliments and then pick one at random.

Here is a list.

compliments = ["You have a wonderful smile", "You are so helpful", "I love how kind you are"]print(compliments)

You would show the learners that a list looks a little like a variable at first, but then we use square brackets and commas to separate list items. The print function is used to show what is inside the whole list.

Write a few lines, then run it to see what happens. You can use this time to ask questions, or ask the class to make predictions.

Next you tell them that you want to just see one item in the list. Show them the piece of code that would allow them to do this.

random_number=int(input())print (compliments[random_number])

Run this code and ask your learners what might happen if you type the number 1 into the user input. They might guess that “You have a wonderful smile” will be displayed. This is a common misconception with lists. Try this out to see what happens.

At this point, you can move to your whiteboard and draw the list with the location numbers above, showing them that in Python, lists begin at 0. Running the code again and changing the input to 0 will demonstrate this.

You could then try entering different numbers.

Now that your learners have an understanding of finding a list item, you can show them how to do this randomly.

The # in the code below shows where new code has been added. The line that prints the entire list has been removed, as it is no longer required.

compliments = ["You have a wonderful smile", "You are so helpful", "I love how kind you are"]random_number = randint(0,2) #print(compliments[random_number])

What’s this? Another error? What have we forgotten to do? Your class should already have an idea of using functions from other libraries, and will hopefully spot that you haven’t imported randint from random. Add this in and run the code again. Run it a few times to show that it really is random.

from random import randintcompliments = ["You have a wonderful smile", "You are so helpful", "I love how kind you are"]random_number = randint(0,2)print(compliments[random_number])

At this point, your learners should have a grasp of:

• How to create a list
• How to call one item in the list
• How to pick an item at random

This is all we need to get us started with a compliments machine. Learners can then adapt and improve the code.

### Discuss

• If you have used live coding before, why did you choose this method?
• What tips have you learnt from this step that will help you with live coding in your lessons?