Let's say there are three 5 A hole , At the beginning, little fox took one of the holes , Then the player opens a hole randomly , If there's a fox in it, it's caught , If there is no fox in it, catch it the next day , But the next day the fox will jump into the hole next door before the player comes . If caught within the prescribed number of times, it is a success .
from random import choice, randrange def catchMe(n=5,maxStep = 10):
""" Simulated catching Fox , All in all n A hole , Allow to catch maxStep second If you fail , The little fox will jump to the hole next door """ #n A hole , There are foxes 1, There is no Fox for you 0 positions =
[0] * n # Random initial position of Fox oldPos = randrange(0,n) positions[oldPos] = 1 # grab maxStep second
while maxStep >=0: maxStep-= 1 # This loop ensures that the user input is a valid opening number while True: try: x =input(
' Please enter the opening number (0-{0}):'.format(n-1)) # If the input is not a number , You'll jump except part x = int(x)
# If the entered opening number is valid , End the cycle , Otherwise, the input will continue assert 0 <=x< n break except: # If the input is not a number , Just execute the code here
print(' We have to follow the routine , Give you another chance .') if positions[x] == 1: print(' success , I caught the fox .') break else:
print(' I didn't catch it today .') # If you don't catch it this time , The fox jumps to the hole next door if oldPos ==n - 1: newPos = oldPos - 1 elif
oldPos== 0: newPos = 0 else: newPos = oldPos + choice((-1.1)) positions[oldPos],
positions[newPos] =0,1 else: print(' Give up , There is no hope for you to try like this .') # start games , Start catching foxes catchMe()