0

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  
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    I guess your problem is that `children` is only set before the while-loop and therefore stays `0` all the time. – Michael Butscher Jan 30 '23 at 18:00
  • What's supposed to happen when `child == 2`? – Barmar Jan 30 '23 at 18:03
  • I was just trying different conditions to get it to run with output other than zero, the children thing fixed my 0 problem but I don't know exactly what condition would run it until I have one of each? – Abby Kabalin Jan 30 '23 at 18:05
  • 1
    If you want to run until you have at least one boy and one girl, you want `or`, not `and`. right now your while loop will break with _either_ boys >= 1 or girls >= 1 (because `false and true` is false), rather than _both_ boys >1 and girls >=1 (because `false or true` is true) – Mike 'Pomax' Kamermans Jan 30 '23 at 18:08
  • Your condition (from your explanation) should probably be `while boys < 1 or girls < 1:`.. Also, `children = boys + girls` should be _before_ `frequencies[children] += 1`. There's also no need to increment `i` with `i += 1`. Your `for` loop is already doing that. – Axe319 Jan 30 '23 at 18:11
  • The shown code will now fail with an error because `children` is not defined. – Michael Butscher Jan 30 '23 at 18:13

0 Answers0