I'm having problems with this while loop. It's not running, I keep getting 0's for all my frequencies. Clearly I'm not implementing it correctly and I'm just getting frustrated. It's supposed to run until I have at least one boy and one girl, then count the frequencies of 2 children, 3 children ect.(Yes I'm aware that a 1D array is just a list but this is how my prof wants me to do it) Any advice is appreciated. Here's my code:
Edit : Fixed the 0 problem, now I don't know what conditions would work so that it runs til I have one of each
Edit2 : Fixed the or problem I think, but I'm getting a list index out of range error, and I can only think that I have too many children when I'm incrementing frequencies but if I have more than 5 children they fall into the '5 or more' frequency
Edit 3: Problem solved! Thanks to the helpful comments, I've got it running how I want it to. The code below is completely fixed
n = int(sys.argv[1])
boy = 0
girl = 1
frequencies = stdarray.create1D(6, 0)
for i in range(n):
boys = 0
girls = 0
while boys < 1 or girls < 1:
child = random.randint(0, 2)
if child == boy:
boys += 1
elif child == girl:
girls += 1
children = boys + girls
if children >= 5:
frequencies[5] += 1
else:
frequencies[children] += 1
boys = 0
girls = 0
Here's an example of what it should look like on the command line:
python boysandgirls.py 1000
Avg # children: 3
Trials with 2 children: 490
Trials with 3 children: 260
Trials with 4 children: 143
Trials with 5 or more children: 107
python boysandgirls.py 1000
Avg # children: 3 Trials with 2 children: 509
Trials with 3 children: 245
Trials with 4 children: 119
Trials with 5 or more children: 127