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

Position the Spaceships Randomly

00:00 Luckily, the challenge text helps us a little bit in how we can actually use this random module for this test. And it says like we should first import the random module, which is part of the Python standard library.

00:15 So you can just import it in line one. And for this task of the challenge, you need to use the random module to pick the spaceships from the spaceships list and save it in a variable named positions.

00:33 Say positions = random. And now you need a method from random. And again, in the challenge text, there was a little bit of help there.

00:42 So if you are not very knowledgeable about the random module, it’s totally okay to just copy that part and paste it here, which was sample.

00:52 And then you need to say from which list you want to pick, which in this case is the spaceships list. And then how many items of this list you want to pick, which in this case is the length of the spaceships list.

01:12 This line is becoming a little bit long, so I will change my editor window so we can actually see everything. Don’t forget the closing parentheses. And then print the positions variable so we can actually see what’s happening there.

01:28 And if what I’m doing right here actually works.

01:32 Once you save and run the module,

01:37 we get two outputs. One is still from our print() function call of spaceships in general. And then you have the output of the random.sample() call, which is basically the spaceship lists, but in a random order.

01:53 So the length argument of the random.sample() call is the length of the spaceships list, which is four. So you get four picks from the list, which is kind of like shuffling the list around, which is what we needed to do for this part.

02:09 So again, let’s clean up a little bit and actually also remove the print() function call for the spaceships in line 10.

02:19 Oh. And also I just noticed that there is a comment which we already tackled, which means saving the picks in the positions list so we can remove that one as well.

02:30 Cool. We tackled basically two tasks with these lines of code and this lesson, so we meet each other in the next one.

Become a Member to join the conversation.