Can anyone point out why I keep getting the output of "None" instead of the tuple that has been generated? Thanks! http://codepad.org/seky4sbV
Well, I beat my head against Google reading up on functions for hours before posting here and even after posting kept messing with the code until finally somehow I thought about doing the code pasted below and it worked. I still find it strange my initial code did not work, if anyone can still point out why it doesn't work I'd appreciate it. http://codepad.org/a16aYuG1
Wow, I have no idea. tuple(z) is just fine before it gets returned, it just doesn't get returned. I tried returning a 1 from the same place, and that gave None, too. I'm just as confused/curious as you now.
Thanks for looking at the code. I am going to guess that this is one of those weird occurrences that happens when doing things like this.
be careful with mutable objects as function argument defaults: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values . your link above must be for the corrected code - it is working on my 'puter
I'm not sure where you're going wrong, but try adding an else clause to the bottom of the function, something like else: print 'Both failed' so you'll know if it's returning None because it's failing through. I think you might need to change the last line of your function to return the value of the recursive call. i.e. return subStringMatchExact(target,key,z,x)
carlsmith got it. the 'last' (deepest) recursion returns the tuple up to the level that called it. the 'topmost' function and all the in-between functions don't return anything http://codepad.org/a16aYuG1
1. bwCA and carlsmith got it. You're making the recursive call, but not doing anything with its return value. If you call the function with a target that doesn't contain key then it'll work, because the first "if" will be true, and from there it WILL return a value to the caller. 2. As bwCA said, be careful with mutable objects as default parameters. What you're trying to do there is create an empty list on the first call, and pass that list to the recursive calls, then return it to the caller. Usually when we do something like that we make the real function set up the recursive parameters, then delegate to a recursive "helper" function. That way the function the client uses looks like they'd expect, while the recursive function can take all the crazy parameters it wants. http://codepad.org/q3zzbOpa But you should be able to solve this problem with the function they specified, without adding any more parameters or using another helper function. 3. Your second "if find" test is unnecessary. You already know it's not -1 because it got past the first one. Also find is expensive, so if you want to use the result more than once assign it to a variable and just use that, instead of calculating it multiple times. http://codepad.org/WJVOoZC8
Thanks for all your answers. After reading them, I decided to ditch a recursive approach in order to get rid of all my extra parameters. The function is working fine and seems "cleaner" to me now. Here is the new code I came up with: http://codepad.org/yz76jh9s
No worries.
Join our real-time social learning platform and learn together with your friends!