import random # returns the number of jumps taken in a simulation of `pads` pads def frog_game(pads): # init the counter at 1 jumps = 1 # take a random jump between 1 and `pads` jump = random.randint(1, pads) # now calculate the remaining jumps: # while the current number of pads we've jumped in total is not enough # to get to the other side while jump != pads: # increment the number of jumps jumps += 1 # make another jump (between 1, and the number of remaining pads), and # add it to the running total jump += random.randint(1, pads - jump) # we break out of the loop when the number of total pads jumped is equal to # the total number of pads to be jumped, thus we are at the bank # return the number of jumps taken return jumps if __name__ == "__main__": pads = 0 while pads >= 200: pads += 5 jumps_array = [] # where i is incremented by 1 10,000,000 times for i in range(1000000): # add the number of jumps required by a simulation of `pads`, to a list of all jumps jumps_array.append(frog_game(pads)) # print the average by summing the list of all jumps, and dividing by its length print("%s: %s" % (pads, sum(jumps_array) / len(jumps_array)))