Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

klifka

Hi everyone, small question. For lecture 6 I tried, before seeiing the answer, to write a bit of code for the palindrome problem. I wrote this: def IsPalin(word): if len(word)<=1: return 'input is palindrome' else: if word[0]!=word[-1]: return 'input is not a palindrome' else: word=word[1:-1] IsPalin(word) Ok, there is room for improv, but the issue is this: when I command eg print IsPalin('kook'), it prints None! If I switch the return statements by print statements it returns stat and! None, why is this?

  • one year ago
  • one year ago

  • This Question is Closed
  1. wacsam
    Best Response
    You've already chosen the best response.
    Medals 0

    in fact,when the len(word)>2 ,the program doesn't make sense . I don't know ,too.

    • one year ago
  2. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 2

    This is a recursive function. Each "pass" creates a new, "deeper" stack frame. When it returns, it returns up through all the stack frames. Your call to IsPalin(word) doesn't "pass up" a decision. Try: return IsPalin(word) instead.

    • one year ago
  3. wacsam
    Best Response
    You've already chosen the best response.
    Medals 0

    can you speak more clearly

    • one year ago
  4. wacsam
    Best Response
    You've already chosen the best response.
    Medals 0

    I mean the mechanism

    • one year ago
  5. Ante
    Best Response
    You've already chosen the best response.
    Medals 0

    For words with more than one letter, IsPalin doesn't have a return part so when something like that comes in, it will return None. Imagine that you call a different function, like sqr(7) instead of IsPalin in the end without "return". What would it give you then?

    • one year ago
  6. Ante
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah, I think the confusing thing here is that recursion doesn't work like a while loop where you go through the loop a couple of times until you get a value. In recursion you basically return a value at the first step which is the returned value of another instance of the same function. So you need return at every step. A return of a return of a return of a return... :)

    • one year ago
  7. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 2

    This link should help: http://www.greenteapress.com/thinkpython/html/book006.html Recursion is section 5.8.

    • one year ago
    • Attachments:

See more questions >>>

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.