In python recursive functions homework I'm required to write a code that prints out letter 's' as a tracing of a sleeping man steps. I wrote my code using two functions.....
import random
def rs():
""" rs chooses a random step and returns it
note that a call to rs() requires parentheses
inputs: none at all!
"""
return random.choice([-1,1])
def rwsteps( start, low, hi ) :
'''
inputs :start --> Starting position of sleepwalker
low --> nonnegative smallest value allowed
hi --> highest value allowed'''
if start <= low or start >= hi :
return
else :
print '|' + ' ' * (start - low) + 'S' + ' ' * (hi - start) + '|'
return rwsteps( start + rs() , low, hi )
That works OK, Now I'm required to add another function to print the number of steps after printing the steps it self.I don't want to count within the function itself.Thank you in advance.
EDIT thank you all I got an idea and its working so here is my new code
import random
def rs():
""" rs chooses a random step and returns it
note that a call to rs() requires parentheses
inputs: none at all!
"""
return random.choice([-1,1])
c = 0
def counter():
global c
c += 1
return c
def rwsteps( start, low, hi ) :
'''
inputs :start --> Starting position of sleepwalker
low --> nonnegative smallest value allowed
hi --> highest value allowed'''
if start <= low or start >= hi :
print c - 1
return
else :
print '|' + ' ' * (start - low) + 'S' + ' ' * (hi - start) + '|'
counter()
return rwsteps( start + rs() , low, hi )
rwsteps( 10, 5, 15 )