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:
return 'input is palindrome'
return 'input is not a palindrome'
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?
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
in fact,when the len(word)>2 ,the program doesn't make sense . I don't know ,too.
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:
Not the answer you are looking for? Search for more explanations.
I mean the mechanism
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?
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... :)
This link should help: http://www.greenteapress.com/thinkpython/html/book006.html
Recursion is section 5.8.