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

Challenge: Space Race

00:00 Welcome to the Challenge. It’s a space race, and you need to start fresh. So don’t work with the code you already created, but create a new file to tackle this challenge.

00:11 Since it’s a challenge, it’s a bit bigger and it’s separated into different slides. So bear with me.

00:18 Create a dictionary named Captains by using this list of pupils: Enterprise, Picard, Voyager. Janeway, Defiant, Sisko. It’s a list with three items.

00:30 The items are pupils, and the first pupil is Enterprise, Picard. The second one, Voyager, Janeway, and the third one, Defiant, and Sisko. And all of the contents of the pupils are strings.

00:44 After you create a dictionary, add another spaceship named Old Besi with Captain Lila to captains. Next, store a list of the spaceship names in a variable spaceships,

01:00 and then the race starts. Randomly pick three spaceships from the spaceships list and store them in a new list named positions.

01:10 For this, you can use the random module and here is a little code example on how to use it if you haven’t used the random module.

01:16 First, you need to import random. Then there is probably some code that you will write on your own. Then you can define a positions list with random.sample, and then in parentheses as one argument, spaceships.

01:31 And then as the second argument, the length of the spaceships list, which is len(spaceships).

01:38 Once we tackle these code snippets in the live coding session, I will explain this code more thoroughly. Of course, after that, you need to create a dictionary named winners with the keys one, two, and three.

01:53 And the default values should be the string unknown for each key.

01:59 And finally, loop over the captains dictionary and congratulate the captain of the ship that made it to the first place and motivate the captain who didn’t make it to the top three, for example, “Better luck next time, Picard. Congratulations, Sisko.” So in this case, Picard didn’t make it to the top three and Sisko made it to the first position.

02:22 Alright, that’s quite a bit, but hey, it’s a challenge. You can stop this video course, take all the time you need. You can download the slides and the corresponding materials that you can download below this video course.

02:34 And once you are finished solving the space race, you can continue with the video course where you can see how I solve this challenge. Good luck.

Avatar image for Jakub

Jakub on Sept. 6, 2024

The challange does not make sens from the first glance. Why we want to pick 3 ships when random.sample will return 4 of them based on len(spaceships)? Also why do we need winners dict when it is not explained in the exercise?

Become a Member to join the conversation.