Step 5 – checking for a match
It's fine checking for a match by eye, but it takes time and you could make a mistake.
Let's get Python to do the hard work for us.
- You need to add this code at the bottom of your program
- The if line is a bit special. I've explained it below if you're interested
- Watch the indentations here
- Run your program to check it works (Run > Run module)
- Run your program 10 times. You should get a match about 5 times
Remember, you won't always get a match. You might only get 3 matches in 10 goes. You might get 7. Chances are you'll get 4, 5 or 6.
THEORY: the IF line works by comparing the length of the birthdays array – len(birthdays) – with the length of the set of the birthdays.
In computing, a set can not have a repeated value. When we convert the array in to a set it simply deletes any duplicated values
So the length of the array might be different to the length of the set
This is a bit of a cheats way of checking things. I'll show you another way of doing it in the extension