# Lesson video

In progress...

Hi, I'm Rebecca, your computing teacher, and welcome to the Programming Part six unit.

Now, this is the final part of a series of six units, aimed at getting you ready to programme your own projects in Python.

So, if you haven't done those other parts before doing this one, it's highly recommended that you do those first and I'll still be here when you're ready to do part six.

But if you are ready, then you're going to need your REPL account, which you should've already set up with your parent or carer' permission, and it's a really good idea to have a pen and paper handy to make notes and to answer any of the questions that I give you in this lesson.

My final bit of advice, try and make sure that you're in a quiet place that you can study if you can, so that you can really focus in this lesson.

Once you got all of that ready, we can begin.

In this lesson, you will describe the record data structure.

You'll use a dictionary to represent a record in a programme, and you'll use a dictionary with a list to represent records in a database.

Let's start by making a prediction then.

Take a look at this programme.

This is something that was covered in Programming Part five.

So, hopefully you remember.

A programme has been started for a battleship style game.

A battleship is represented by a B, and you can see the B there in the data structure.

Which indices are required to reveal the battleship? So, take a look at those lists of options and pause the video while you make your prediction.

Okay, so here is the answer.

So, it was [2][1].

So, if you take a look at it, you do the rows first.

So, you've got row 0, is the top one, then row one, and then row two.

So the B is on row two, and then if you look at the columns next.

So, you go zero first and then it's in column one.

Now we're going to be looking at another data structure in this lesson, and it's called a record.

Data structures are used to store data in an organised and accessible way.

A list is a data structure.

Another example of a data structure is a record.

You might have heard of the word record if you have ever created a database before.

A record allows you to store a collection of attributes for a single entity.

Here is an example of a record that holds the data for a player.

Here is the entity, and here are the attributes.

An entity can be any object, place, person or thing.

Attributes are properties or characteristics of that entity.

Attributes are sometimes referred to as fields.

A database will hold many records for a particular entity.

Can you think of any other entities that we could store in a record? Pause the video while you have a little think about that.

Here's some ideas then.

So, is it possible things that you could have said so, for customers, for books, for games, stock, cars or devices, so you might've had something a bit different, but anything that stores data on a collection of things can be used in a database and can store the records.

A record can be represented as pseudocode, like this example here.

So you have the entity at the top, so the record is for that player, and then the attributes are listed underneath.

A record is a static data structure.

You must determine the attributes and the data types for each entity during declaration.

These will then be fixed for each record used in the database.

Python does not have a native data structure for a record.

Instead, we can use a different data structure called a dictionary to represent a record.

A dictionary has some of the features of a record.

Here is an example of the dictionary data structure being used as a record.

So here is that entity, here are the attributes, and here is the data.

Note how curly brackets are used for dictionaries in comparison with square brackets, which are used for lists.

A dictionary works differently to a list.

In a list, items are accessed by their indices or their index location.

In a dictionary we access the data using a key.

If you want to know the data associated with the username key in the player's record, we use this.

What will be the output for this programme, do you think, based on what I've told you there? Pause the video while you have a think.

Let's take a look.

So the output will be rockstar.

To modify the data paired with the key we can use this line of code.

So what will be the output for this programme, do you think? Pause the video or you have a think.

Let's take a look.

So the seven goats eating.

It's a strange password, isn't it? So what I'd like you to do, 'cause you've, you've watched a bit of a demonstration there you've seen some example code, but until you start actually playing around with it in Python yourself, it doesn't become clear to you.

So it's good to have a little practise.

So what I'd like you to do is to use the Make a Record section of your worksheet to create a record and practise accessing and modifying attributes.

So have a go at that, pause the video.

Excellent, so here's some of the answers then.

So this is for Create a Record.

So just pause the video while you have a go at that.

You had to do your own want to think, so you might have something slightly different, but that would be the structure.

And then this would be for the answer for Access and dispLay the data in your dictionary.

So here's some sample code you could have used there.

Remember, you might have done things slightly differently and that's okay as long as it's working.

And then this one was for Modify the dictionary so that it contains new data.

So pause the video, check that with your code.

And if you're struggling with any of those, you can always go back, just rewind and pause the video where you need to and just check your code with that one and try those bits of code as well out in Python too, just so that you can get a really good understanding of how it's working.

So now we're going to look at adding a dictionary to a list.

A database can hold multiple records as seen in the example here.

The database players has three records.

If we want to use multiple records in a database, then we can add multiple dictionaries to a list, and you can see that happening there.

So there is the line of code where those three dictionaries are added to that list.

Note that each dictionary follows exactly the same fixed structure.

We have just changed the data and the names for each dictionary.

So what I want you to do now is use the Make a Database section of your worksheet to make predictions about a programme that builds a database using dictionaries and lists.

So read that carefully, have a really good go at it, and I'll go through it with you in a few moments.

So pause the video while you have a go.

Excellent, so let's take a look how you could have solved this task.

Let's go to REPL.

So I'm going to go through those investigate and modify tasks with you now, just so that you can see how I would have answered those questions and check them with yours.

Now I've got the worksheet printed off in front of me.

So that's why I'm looking down and reading off a piece of paper so that I can go through it with you.

So if you wonder why I'm looking down, that's what I'm looking down for.

So the step one then was, it says lines six, eight, and 10, all prompt for input that will be held in three variables.

So where is it? So you've got six, eight and 10, so yeah, you can see those three variables there and they've got input.

And it says, where else in the programme are these three variables being accessed.

So let's have a look.

So we've got here, and I think, it's just those three places, isn't it? Yeah, so in the dictionary, on lines 12, 13 and 14, it says, where is the players list initialised? So where can I see that, right? That is right at the beginning, isn't it? Right at the top there, 'cause it initialises when it's first set up so we can see it being initialised there on line one.

Where is the new data added to the players list? So now we need to see where it's being referenced.

So where is players being referenced? Ah, there we go.

So we've got players.

append there.

So that line of code there is adding whatever is being held in player to the list players.

So that's what's happening there.

What new data is being added to the players list on this line? Well, actually it's everything in this dictionary here.

So all of this, the username, password and the score, and it says, what condition needs to be true in order for the while loop to terminate? So let's go up here and we've got line four is where the condition is.

We got add players, initially add players is set to true, it's a Boolean condition there and we want to see where would it be false? Where would it terminate? So actually if we look down here, we got, if answer is equal to no.

So that condition needs to be true in order for the loop to terminate.

So that one on line 20.

And then step seven, enter two new records during execution, before typing and when prompted, what is the output When line 23 is executed? So I'm going to run the programme now, it says enter a username, so I'm going to put Pi.

Enter a password Raspberry, enter a score 100.

Would you like to add another player? Let me just look.

It says to enter two I think, yeah.

It's a to enter two.

So I would put yes, enter a username, Google, enter a password, Red, enter a score, 65.

Would you like to add another player? No, 'cause I've added two now and then it's displayed as output.

All of that data that I've entered in here.

So what is the output when line 23 is acted? Well, it's just displayed those two dictionaries that have been set up and they are inside a list.

So you can see the list structure because you've got that square bracket either side, and then you can see the dictionary structure, 'cause you can see the curly brackets there and then the comma separates each element in that list there.

So there's two elements in the list.

So then it says to start modifying the programme.

So it says the programmers realised that they have left out an attribute from the record.

They need to have an attribute for highest score, modify the programme so that the user can input a highest score, which is then added to the dictionary.

So I've got a look here.

If you look here, this is where they've, where they enter their username, their password and their score.

So now I can just add another one to the bottom, so print enter your highest score.

And then I can have a variable called highest score and then I can have an input like so, and then I've got to also add it to the dictionary as well.

So I got to look really carefully at the structure that's been used for this dictionary.

And you can see that you have the string there like that.

Then there's a space with a colon and then we've got the variable name, which would be highest score.

And you can see that there is a comma where we're going to add another one underneath.

But on that final one, there isn't a comma.

So I've got to put a comma here because I'm adding a new one.

And then I want highest, spell that wrong, highest score.

And I'm going to put those, the colon.

Oh, I haven't finished it with the speech mark.

And then I'm going to put highest score.

And just that definitely isn't a comma there at the end.

So I think I've set that at right.

But is it going to display it? I think it should be.

Yeah, it should display it now.

Yeah.

I think that's going to work.

I think we just got to test it though.

So take a look at product.

Now it doesn't tell me in the instructions to test it, but I'm going to test it anyway 'cause I always test everything to make sure it works.

Enter a score, 100, enter a highest score, 120.

Would you like to enter another player? No.

So if you look now you can see that we've just got one last item, which is the whole dictionary in this, in this list ' cause you can see the square bracket there and the square bracket there.

And there's no commas to separate those elements of those dictionary and that's the whole dictionary there.

With all those bits, the username, the password, the score and the highest score, so it's worked, we know it's worked and then it says the programme needs to access the password for the dictionary that is located at position zero.

At the bottom of the programme, write some code that will display the password.

And it gives you some hints as well because no doing this before is you've got to make sure that you know which code you're going to use, so the hints are quite helpful.

The dictionary in location zero of the list, doesn't have an identifier.

You can assign it to a variable so that you can refer to it, look back at your work from the first task to see how to access the data pairing for a given key.

So let's just revisit that question again while we thinking what we need to do.

So the programme needs to access the password for the dictionary that is located at position zero.

So how do we access just that first one at position zero, the dictionary position.

We know what we're going to do for that, and that's just going to to be.

I'm just going to print it.

So if we wanted to just print that first location in that list, it's the players list, isn't it? That's what we've called it yeah, we called it a players list.

So you could just have players zero.

So that is how you access just that first item in that list.

But now we need to know how we access the dictionary, the key pairing for the password.

So I just found a code snippet to help with this, and this will display one attribute of a dictionary.

So I need to use this to help me with this but now we've got here print , and this is just going to access the dictionary that is at location zero.

Now it's going to be really handy if I have a variable actually that holds this whole dictionary.

So I'm just going to put, what should I call it? Current player maybe, I'm going to call it player.

So I'm going to call it player, like, so, and now if I use this as my structure, so what's the actual question asking? It wants to access the password.

So if, instead of having username here, I have password, I just check the Oak spelled correctly, and how it's listed in here.

So what they should do now is we'll look at the list item that is in zero and it'll assign that to the variable player.

And then the, this part of code will access the attribute password and it should display the password.

So let's see if that works.

So I'm going to put two records in this time, just so that I can see that it is only accessing that first one.

So I'm going to put Raspberry, Pi, 100, 120 and then go put another one.

Yes, Google, Red, 70, 80, no.

So then it's displaying the whole thing.

Let's just a look at what it's, what it's doing.

It's displaying the whole thing and that's because I've still got this bit of code here.

I should have hashtag that out, oh well.

And then the final thing, it should just print the password for the record that is at location zero.

And it has like you can see Pi there at the bottom.

So it has worked.

I should have just hashtag this out, this bit here, so that I can see it properly.

But it's definitely working.

So that's good.

So the next step then, test it works correctly.

Hold on, we got the right one? The programme? Yeah.

So modification four is test that your programme works correctly, if you input the following, the final output should be housed.

So I've already tested it, so I don't need to test it again.

Then it says, modify the programme so that the user can choose which record they wish to access by typing in the index.

If the user types a zero, then it should display the entire record at location zero.

Okay, so do I still want to keep this? I might just keep this code here.

The only thing is I might.

No, it should work, it should still work.

So this is my brain ticking over if you're hear me talking to myself, but that's just my brain ticking over.

I'm just going to, in fact, I'm going to hashtag out that's what we're going to do.

Okay, so it's useful to hashtag things out while you messing about.

So what he wants me to do is if the user types a zero, then it should display the entire record at the location.

So almost pretty much got this code here, but instead of having zero there, what I look to do is put a variable.

So I'm going to say, which record would you like? Like so, and then I'm going to have record input like this.

And then if I actually keep this bit.

Yeah, I want that bit.

And I want, no, I don't.

No I want to print it, don't I? So I want to print players and then I want to print it at the record location.

Now I have made a purposeful error there.

I want you to see if you can figure out what it is.

Pause the video while you have a little think.

Let's run the programme and see what happens.

So enter a username, Raspberry, Pi, 100, 120.

Would you like to add another one? No.

Which record would you like? Zero.

Okay, so I knew I made a mistake.

I wonder if you spotted it as well.

So let's take a look.

List indices must be integers or slices, not string.

So what did I miss out from this bit here? I haven't carted it to a string.

So if I actually put str, no integer, not a string, integer.

I'm like, so if I do that, it should work now.

Let's just have a look.

So Raspberry, Pi, 100, 120, no, which record would you like? Zero.

And now it's worked because I've got that integer there because it needs to have an integer value there.

So that bit's working.

I don't like how that appears with that space though.

I might just put that like that.

Okay.

So say my front programme so that the user can choose which record they wish to access, yes, I've done that.

Modify the programme so that user can choose which attribute they wish to access by typing the name of the attribute.

And you got a hint there.

If the user types high score then it should display the highest score of their chosen record.

So I could use a selection statement here, because the clue is in the hint there, so if the you typed high score, then it should display this.

So we know we've got these options here, to get it to display a certain attribute.

So we've got this bit of code here that we can use.

So if I type in zero, then it's going to play that.

And then I can say print which attribute would you like? Like, so.

And then I'm going to have to put another variable called attribute and then store their answer.

And this time it will be string.

So I've got, if attribute is equal to, what's the first attribute username.

I'm going to figure that bit out in a second.

Elif attribute equal to, which one was it? Score, we got, score me bought highest score.

There's other ways that you could have done this as well.

Okay, so now we've just got to think, well, what needs to be displayed in here? We've got this bit of code here that displays an attribute.

So it's going to be this basically, isn't it? But just varied, so copy.

I'm going to paste this in, I'm going to do it like this.

Go make sure I've got the tap, then the final one, like so.

And then I've just got to change what's in here so that one's going to be username, that one's already right.

This one's going to be score, and this one's going to be highest score now.

Is it like that in there though.

Let me just check.

Yeah, it is.

Okay, so now we test it again.

So like is it going to display what I wanted to? Yeah, it should do.

So let's just take a look at it now.

So enter a username, Raspberry, Pi, 100, 120.

Yes, no I'm going to put no.

And then I want to, which record would you like? Zero.

Which attributes would you like? And I'm going to put password, and it says Pi, so it's fully working, okay? You might have done it slightly different to that, and not just how I tackled it, this time.

As long as it's working as is expected.

We have covered a lot of new keywords in this lesson and it can be quite hard to remember all of them.

So please don't worry if you don't get this right, this is just to help jog your memory.

And the more you use these terms, the better you'll get remembering them.

So what I'd like you to do is pause the video and see if you can remember what some of these keywords actually mean.

Off you go.

Right, let's take a look at the answers then.

So the first one was record, so this allows you to store a collection of attributes for a single entity.

An entity is a person, object, place or thing that has characteristics.

An attribute, the properties or characteristics of any entity.

A database is used to organise and store multiple records.

And a key is used to link an attribute to a data pairing.

Excellent, so you've learned about the data structure, a record and a dictionary in this lesson.

So quite a lot to be getting on with, and try and to understand as well.

And we'll be looking more into dictionaries next lesson, where you will have a bit of a challenge to do too.