Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Last active December 20, 2015 03:49
Show Gist options
  • Save robrocker7/6066482 to your computer and use it in GitHub Desktop.
Save robrocker7/6066482 to your computer and use it in GitHub Desktop.
Very Basic Recursive Function Example in Python
# Example recursive
import random
def recursive_check(loop=1):
""" This is a very basic recursive example.
Each recursive loop grabs a random number between 1 and 10 and checks to see if its 7.
The loop will record how many attemps it takes and return the number when the random integer is 7.
"""
random_int = random.randrange(10)
if random_int == 7:
print 'Number was 7! Took {0} steps'.format(loop)
return
print 'Number was {0}'.format(random_int)
recursive_check(loop+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment