List All Spaceships
00:00 You have the Captain’s dictionary in place. Now you need to create a list with the spaceship names. And here my comment says list in the end, so I can scroll to the right store spaceship names in a spaceship list.
00:17
We know the name of the list, which should be spaceships
.
00:23
And then the content should be the spaceship names. That’s basically the keys of the captains dictionary. So let’s see what happens when we just say Captains
.keys()
as the value of the trips
variable.
00:41
Let’s print space_trips
in the next line
00:46 and save and run the module.
00:51
And then you can see, well, there is something that looks a little bit like a list, but it’s actually a dict_keys
object at this point. To actually make it a list, you need to provide this dict_keys
object into a list()
function call.
01:07
So you need to update line 11 where you define the space_trips
variable. And before Captains.keys()
, the list()
function call with an opening parenthesis, and at the end of the line, the closing parenthesis.
01:21 And then when you save and run the module, then you can see that the output in the shell is actually a list that contains Enterprise, Voyager, Defiant, and Old Bessie, which are the names of our spaceships.
01:38
So again, you can remove the comment and actually let’s remove also the print()
function call of captains
because we don’t need that part anymore.
01:48 So we save a little bit of space in our editor, and with this, we can continue to the next lesson where we put a little bit of randomness in our script. So let’s see how to tackle that.
Become a Member to join the conversation.