python如何循环功能

时间:2018-09-25 02:26:01

标签: python for-loop counter

我的代码如下,在Windows操作系统上使用spyder

import numpy 
catch_rate = 0.5 
def tenMiss(throws):
    for i in range(len(throws) - 3): # loop through the throws
        print(sum(throws[i:i+3] < catch_rate))
        if sum(throws[i:i+3] < catch_rate) == 0: # total misses out of 10 throws
            print("ten Misses!")
            return True 
        return False
throws = numpy.random.random(20)
print (tenMiss(throws))

我正在尝试使用“ for”循环将该函数循环700次,然后添加一个count变量以确定该函数返回true的次数。我遇到的麻烦是确定该变量应进入for循环以“选择”此函数为循环。一旦到达那里,我将如何合并计数变量

1 个答案:

答案 0 :(得分:1)

true_count = 0
for x in range(700):
    result = some_function()
    if result is True:
        true_count += 1
print('True was returned %d times' % true_count)