Trying to do the Birthday Program in python. Being a beginner in Python, I am having some trouble.
- Function duplicates(l) that takes a list l and returns True if it has a duplicate element, and False if it does not.
- Function test(count) generates a list of count random integers between 1 and 365 . Function duplicates(l) will test for duplicates.
- Function probability(count, num) runs num tests of count people, and counts the number of tests with duplicates. It returns the fraction of the tests with duplicates – the number of duplicates divided by num.
results should look like:
For 2 people, the probability of 2 birthdays is 0.002.
For 3 people, the probability of 2 birthdays is 0.008. and so on…
Stuck on step 2 & 3:
import random
# not sure what to put for count
count = [random.randint(1, 365)]
def duplicates(l):
if len(l)!=len(set(l)):
return True
else:
return False
def test(count):
return [random.randint(1, 365)]
#def probability(count,num):
I believe I did step one correctly, but I’m not sure where to go from here.
You should create the list like this.
testisn’t a very useful name. Consider something likemake_birthday_listinsteadThe test in duplicates already evaluates to a bool, so you can just do this. It’s not a good idea to use
las a variable name (looks too much like1) so I changed it tothe_listYou’ll have to run the test over and over to get the probability
eg.