Buy:
|
Secret Santa
How it works
The program works by picking a name at random from a list and then pairing them up with another name from the list. Each person must keep their pairing a secret and either buy or make a gift for the person they have been drawn with. For example:
Kyle is paired with Aiden
Millie is paired with Dave
Archie is paired with Oliver
Kyle is paired with Aiden
Millie is paired with Dave
Archie is paired with Oliver
Instructions:
Start by entering the following code in the code window below:
Start by entering the following code in the code window below:
#Sorting hat
import random list = ["Gryffindor","Hufflepuff","Ravenclaw","Slytherin"] #function to pick a house at random def pickHouse(): #pick a random position in the list index = random.randint(0,len(list)-1) house = list[index] return house houseName = pickHouse() print("I choose " + houseName) |
Challenge 1:
Modify the code so that it picks a name from your class at random.
Challenge 2:
Currently, your random name picker can pick the same name several times. Let's modify your code so that it removes a name from the list once it is picked so that the name cannot be picked again.
Add the following code before the line 'return house'
Modify the code so that it picks a name from your class at random.
Challenge 2:
Currently, your random name picker can pick the same name several times. Let's modify your code so that it removes a name from the list once it is picked so that the name cannot be picked again.
Add the following code before the line 'return house'
del list[index]
|
Your code should look a little something like this:
#Random name picker
import random list = ["Alex","Dave","Kyle","Millie"] #function to pick a name at random def pickName(): #pick a random position in the list index = random.randint(0,len(list)-1) randomName = list[index] return randomName name = pickName() print("Name: " + name) |
Final challenge:
Modify the code so that it picks two names at random from the list and prints out the names. For example:
Alex is paired with Kyle
Millie is paired with Dave
Modify the code so that it picks two names at random from the list and prints out the names. For example:
Alex is paired with Kyle
Millie is paired with Dave
Worked example:
secret_santa_worked_example.zip |
Example solution:
secret_santa_solution.zip |
Taking it further
Modify your code so that that it pairs everyone in the list (whole class).
Top tip: You will need to include a loop. For example:
Modify your code so that that it pairs everyone in the list (whole class).
Top tip: You will need to include a loop. For example:
for i in range(2):
name1 = pickName() name2 = pickName() |
Bonus Challenge
Create a random group picker that places people into groups of 4 or 5.
Create a random group picker that places people into groups of 4 or 5.