0

This is my code, while I am running my function rwpos() is supposed to return something, but it is not returning anything. Please help me through this...

import random

def rs():
    return random.choice([-1,1])

def rwpos(start,nsteps):
        if nsteps == 0:
                print start,nsteps
                return start
        else:
                start = start + rs()
                rwpos(start,nsteps-1)

x = rwpos(40,4)
print x

x is printing None ... ~

vaultah
  • 44,105
  • 12
  • 114
  • 143

1 Answers1

1
import random

def rs():
    return random.choice([-1,1])

def rwpos(start,nsteps):

    if nsteps == 0:
        print start,nsteps
    else:
        start = start + rs()
        return rwpos(start,nsteps-1)

x = rwpos(40,4)
print x

You were not returning in the else statement.

Nathan M.
  • 286
  • 1
  • 9
marsh
  • 2,592
  • 5
  • 29
  • 53