-1

I have a variable "x" assigned to a function ("rundown"). Inside of that function, two variables "y,z" are reduced by a random value. Conditional statements then check if both y and z are still greater than 0 - if so, the function runs again with the new values, repeating until one variable hits 0.

Once one of these variables hits 0 or less, the function is supposed to return a value based on whether y or z hit 0 first. x is supposed to be assigned that value so conditionals in the main function can be based off the result of the rundown function. But that part doesn't seem to be working.

I've tried adding print statements to the conditionals just to see if they trigger, and they do. But when I add a print statement to x, it just prints "none" so I know that's where my code is breaking. I'm just not sure why... Here's the jist of what I'm working with:

def main():
  x = rundown(y,z)
  if x == 0:
    #(do something)
  elif x == 1:
    #(do something else)


def rundown(y,z):
  y -= (1 + random.random())
  z -= (1 + random.random())
  if y <= 0:
    return(0)
  elif z <= 0:
    return(1)
  else:
    rundown(y,z)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 6
    Duplicate of dozens of others, e.g. http://stackoverflow.com/q/15340281/3001761, http://stackoverflow.com/q/22311440/3001761, http://stackoverflow.com/q/17778372/3001761 - you don't `return` in the recursive case. Also note that `return` is a statement, not a function - just `return 1`. – jonrsharpe Apr 23 '16 at 21:54
  • Hi sorry, noobie here, not quite grasping this. So, the recursive case is the else conditional in rundown where the function calls itself, correct? Are you saying I need to return a value in that conditional? – J. Bridgman Apr 23 '16 at 22:09

2 Answers2

-1

isn't the rundown under else going to return a value with in the function but you don't have it assigned to anything like you do in the main. Maybe do

return(rundown(y,z))

Mat000111
  • 24
  • 2
-1

forgot return in the else statment return rundown(y,z)

Donat Pants
  • 379
  • 4
  • 11