尝试 运行 测试以查看每个性别需要多少 children,但数组无法正常工作?

Trying to run a test to see how many children it takes to have one of each sex but the array is not working properly?

我是 运行 class 的一些代码,但我不知道该怎么做,我提交了作业,但如果我不知道最后该怎么做,这对我没有帮助.这是我 运行 尝试跟踪这些测试的代码。这个想法是看看在每个性别至少有 1 child 之前需要多少 children。然后程序应该将其存储到列表值 boysandgirls_stats[x]。问题是它不会存储所有测试。它只记录 100 个中的 40 个或 1000 个中的 200 个。我不知道这些其他测试存储在哪里。 stdarray 是我们必须从我们正在使用的书中使用的东西。它跟踪测试的方式类似于 python 中的列表,但我没有很好地解释它。谢谢!

import stdio
import random
import sys
import stdarray

numTrials = int(sys.argv[1])

boysandgirls_stats = stdarray.create1D(4, 0)
boys = 0
girls = 0
children = 0
totalChildren = 0


for x in range(numTrials):  # Simulation to determine sex
    childType = random.randint(0, 1)
    if childType == 0:
        boys += 1
        children += 1
        totalChildren += 1
    else:
        girls += 1
        children += 1
        totalChildren += 1
    for x in range(numTrials):  # Keeps track of amount of children needed
        if (boys >= 1) and (girls >= 1):
            if children <= 5:
                boysandgirls_stats[children - 2] += 1
                boys = 0
                girls = 0
                children = 0
            else:
                boysandgirls_stats[3] += 1
                boys = 0
                girls = 0
                children = 0
avgChildren = totalChildren / numTrials  # Calculation for average children
stdio.writeln('Avg # of children: ' + str(avgChildren))
for x in range(len(boysandgirls_stats)):
    stdio.writeln(str(x+2) + ' children ' + str(boysandgirls_stats[x]))
import sys
import random

## let's put the test inside a function:
def child_test():
  girl = False
  boy  = False
  total_children = 0
  while not (boy and girl): 
    total_children += 1
    if random.randint(0, 1) == 0:
      boy = True
    else:
      girl = True
  return total_children


## now the statistics:
# get the number of trials from user command:
num_trials = int(sys.argv[1])

# for every number of children, log the associated number of tests:
total = 0
indexed_count = {}
for i in range(num_trials):
  num_children = child_test()
  if num_children not in indexed_count:
    indexed_count[num_children] = 0
  indexed_count[num_children] += 1
  total += num_children

# calculate and output the average:
average = total / num_trials
print(f'average is : {average}')

# show tries per number of children
sorted_indexed_count = sorted(indexed_count.items(), key = lambda x: x[1], reverse = True)
for t in sorted_indexed_count:
  print(f'  {t[0]} children : {t[1]} tests')

输出:

$ python3 so.py 100000
average is : 3.00812
  2 children : 49702 tests
  3 children : 25238 tests
  4 children : 12401 tests
  5 children : 6314 tests
  6 children : 3159 tests
  7 children : 1528 tests
  8 children : 840 tests
  9 children : 410 tests
  10 children : 214 tests
  11 children : 99 tests
  12 children : 53 tests
  13 children : 18 tests
  14 children : 10 tests
  15 children : 6 tests
  16 children : 6 tests
  18 children : 1 tests
  17 children : 1 tests