Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Start Congratulating Winner

00:00 The important part for this task is that you want to loop over the Captain’s dictionary. So with a few dictionaries in place now, it’s good to not get confused like I did in the last lesson.

00:11 So we are looping over the Captain’s dictionary and you want to congratulate the captain who made the first place and motivate one who didn’t make it to the top three.

00:22 So we need, again, the key and the value of the dictionary, which means it’s a good idea to use the .items() dictionary method for. And then the first variable of the tuple will be the key, which are spaceship names.

00:39 So I declared this variable as spaceship, and the second part of this tuple that the .items() method returns is the value. So in this case, it’s the captain’s name.

00:52 So I use this variable name captain again in captains. That’s the dictionary .items().

01:02 And to first verify that this is actually what I want, let’s print the spaceship and the captain.

01:12 So when you save and run the module, then let’s have a look at the output in the shell, which is in the end, Enterprise, Picard, Voyager, Janeway, Defiant, Sisko, and then Alt Pesi and Lila, which is basically the order of the key-value pairs in our Captain’s dictionary, but it’s not the order of the space race.

01:37 So now we need to do a little bit of fiddling there to actually congratulate the winner of the space race and motivate the one who didn’t make it to the last three.

01:49 Let’s tackle the winner first.

01:52 You know who the winner is because we have the winners dictionary and the winner is on position one there. So you can use the square brackets here and ask for the key one, and let’s just print it.

02:09 So this will be printed in every step of the for loop, but that’s okay. For now, we just want to see like who is the actual winner, which is Enterprise.

02:19 Now we need to check if the spaceship name of our variable spaceship is the value of the key one. In winners. We need an if statement here.

02:35 If spaceship equals the value of the key one in the winner’s dictionary, then we only want to print then the spaceship and the captain’s name.

02:47 So let’s try this out, save it, and run it.

02:53 And as you can see in the last line, it prints out Defiant and Sisko, which is the spaceship name in the first position. So that works out fine.

03:03 Currently, you’re not printing anything else. So let’s create an else statement.

03:11 And again, print a spaceship and the captain. So that’s basically a little bit redundant right now, but we’ll get into it more and more with every step.

03:22 So let’s run the module again. So now again, we get all the others, but the first one should be at least in our code now be a bit more visible as the winner.

03:33 So we can create an f-string and say Winner: And then with curly braces, spaceship, comma. And then it’s also a good idea to put captain into curly braces and close the string off with quotes.

03:49 And once you’re safe and run the module, we get the output for all the spaceships that are not winners. And then in between there, there is a winner, which is Defiant Sisko in the situation.

04:02 Since you’re using the random module there, the output is different every time, and it can be different when you run it. And it can be different every time I run it.

04:11 Sometimes it stays the same, but it’s basically just because we are not using many values. So it was, again, Defiant Sisko winning this space race.

04:24 The code looks good so far, but we are not there yet. So let’s tackle a few more things in the next lesson.

Become a Member to join the conversation.