A community for students.
Here's the question you clicked on:
 0 viewing
 3 years ago
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
 3 years ago
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

This Question is Closed

saclark12000
 3 years ago
Best ResponseYou've already chosen the best response.0Well, 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

jedric
 3 years ago
Best ResponseYou've already chosen the best response.0Wow, 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.

saclark12000
 3 years ago
Best ResponseYou've already chosen the best response.0Thanks 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.

bwCA
 3 years ago
Best ResponseYou've already chosen the best response.1be careful with mutable objects as function argument defaults: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#defaultparametervalues . your link above must be for the corrected code  it is working on my 'puter

carlsmith
 3 years ago
Best ResponseYou've already chosen the best response.0I'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)

bwCA
 3 years ago
Best ResponseYou've already chosen the best response.1carlsmith got it. the 'last' (deepest) recursion returns the tuple up to the level that called it. the 'topmost' function and all the inbetween functions don't return anything http://codepad.org/a16aYuG1

dmancine
 3 years ago
Best ResponseYou've already chosen the best response.21. 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

saclark12000
 3 years ago
Best ResponseYou've already chosen the best response.0Thanks 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.