Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Single Out the Last Place

00:00 Currently, we’re printing out all the other captains that are not a winner, but in the challenge task, actually, we want to motivate the captain that didn’t even make it to the top three.

00:13 So we don’t care about the places two and three, sorry about that. We only care about the winner, which we tackle already, but you need to address the captain that didn’t make it to the top three even.

00:26 So instead of just using an else statement there, let’s define this line of code a little bit better. And instead of using else, we’ll create an elif statement.

00:40 So if the spaceship is not the winner, but elif so, else if, but if this spaceship is not even in the winner’s dictionary, then we want to motivate this captain.

00:54 So you use elif spaceship not in. And then we need to check if the spaceship name is in the values of the winners dictionary, which you can do by using the .values() dictionary method, elif spaceship not in winners.values().

01:13 And then let’s have an f-string down there as well.

01:17 Let’s say not in winners.

01:25 We’ll adjust this in a moment to actually have a nice string there. But for now, let’s just check if the output is what we expect. So an f-string and put spaceship and captains into curly braces to form them into the string.

01:42 Don’t forget the quotes in the end. And now when we save and run the module, you can see that the winner in this case is Voyager, which is the value of the first key in the winner’s dictionary.

01:57 And Defiant didn’t make it to the winner’s dictionary. It’s in the fourth place of the positions list, which then gets the not in winners print.

02:07 So that’s actually pretty nice and that’s almost the solution to the challenge. But the challenge wants us to print this a little bit more nicely.

02:20 For this, I clean up the code a little bit and remove the other print function calls

02:27 and give my editor a bit more space because the f-string will be a little bit longer maybe. Let’s see, instead of printing winner and then the spaceship name, we want to say congratulations.

02:43 And then actually only the captain, not the spaceship, but we can add an exclamation mark at the end to celebrate him a little bit. And instead of just saying not in winners, we want to say Better luck next time.

03:02 And then also the captain after the comma. And let’s put a little bit of a disappointing period in the end instead of an exclamation mark. And then when you save and run the module,

03:16 and then it says, Better luck next time, Janeway. Congratulations, Leila.

03:20 Let’s run again to see that it’s actually random. What happens there? Better luck next time, Janeway. Congratulations, Cisco.

03:29 So there is a bit of randomness in there. You can run this module more and more to have different space races. And that concludes this challenge. I’m curious if you solved it the same way or if you have a bit of a different solution, please let me know in the comments below.

03:45 Because there are many ways of winning this space race, and there are many ways of solving this challenge. Before we’re wrapping up this course, I will give you a few additional resources in the next lesson where you can dive in a bit deeper into certain concepts that are used during this course.

04:04 See you in the next and last lesson.

Avatar image for roshny

roshny on Oct. 17, 2024

from random import sample

captains = dict(
    [
        ("Enterprise", "Picard"),
        ("Voyager", "Janeway"),
        ("Defiant", "Sisko"),
    ]
)

captains["Old Bessie"] = "Leela"

spaceships = [ship for ship in captains]
# spaceships = list(captains.keys())

positions = sample(spaceships, k=4)

winners = {place: "unknown" for place in range(1, 4)}

for i, ship in enumerate(positions[:3], start=1):
    winners[i] = ship

for ship, captain in captains.items():
    if ship not in winners.values():
        print(f"Better luck next time, {captain}.")
print(f"Congratulations, {captains[winners[1]]}")

Become a Member to join the conversation.