Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

saclark12000

  • 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
  1. saclark12000
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  2. jedric
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. saclark12000
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

  5. carlsmith
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  6. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

  7. dmancine
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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

  8. saclark12000
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  9. carlsmith
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    No worries.

  10. dmancine
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    http://codepad.org/JKC5w5aZ

  11. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.