[Tutor] Error message...
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu Aug 23 18:36:14 CEST 2012
On 23/08/2012 16:33, Victoria Homsy wrote:
>
>
>
>
> Dear All - sorry to bother you. I just tried to run this program:
>
>
> def isPalindrome(s):
> if len(s) <= 1: return True
> else: return s[0] == s[-1] and isPalindrome (s[1:-1])
> isPalindrome('aba')
>
>
> However when I run it in terminal it doesn't give me any answer - True or False. (I want the program to tell me whether the input string is True or False). In order to get an answer, I assume I would need to tell the program to print something. However I'm not sure where in the program I would do this. I tried this:
>
> def isPalindrome(s):
> if len(s) <= 1: return True and print "True"
> else: return s[0] == s[-1] and isPalindrome (s[1:-1])
> isPalindrome('aba')
>
> However, this does not work - I get another error message.
>
> Could somebody advise what I'm doing wrong here? Thank you.
You're not spending enough time thinking, seriously. In your original
attempt you've got isPalindrome which returns True or False. You call
the function but don't do anything with the return value, so it's simply
discarded. Then you mess around with a perfectly good function instead
of fixing the real problem. You've two options. The simplest is :-
print 'isPalindrome returned', isPalindrome('aba')
The alternative which is used when you want to keep using a return value
is :-
status = isPalindrome('aba')
print 'isPalindrome returned', status
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Whoops might have helped if I'd hit "Send".
--
Cheers.
Mark Lawrence.
More information about the Tutor
mailing list