[Tutor] self keyword in recursive function
Alan Gauld
alan.gauld at btinternet.com
Sat May 31 10:32:09 CEST 2014
> On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav
> <ritwikraghav14 at gmail.com <mailto:ritwikraghav14 at gmail.com>> wrote:
> and the error is:
>
> Correct Return Value: No
>
> Answer check result:
> Result must be not null.
>
> Execution Time: 0.017s
>
> Peak memory used: 24.551MB
>
> abnormal termination (exit 1)
>
> Standard Output:
>
>
> Standard Error:
> Traceback (most recent call last):
> File "Wrapper.py", line 182, in <module>
> AttributeError: 'module' object has no attribute 'PersistentNumber'
This is not standard Python error output so I assume it has something to
do with how you submit code to the site you are using. It looks like it
has some kkind of testing framework that your code must comply with.
You probably need to read their guidance on code submission closely.
One possible bug I did notice in your code is here:
count = 0
You define count outside the method but not as part of self.
def getPersistence(self,n):
product = 1
if len(str(n)) == 1:
return self.count
then you return self.count. That's a different value, which may be
defaulting to None in your test environment? If you insist on using
recursion I'd bring the count back as an argument defaulted to zero
as you did in your original code.
else:
a = str(n)
for i in a:
product *= int(i)
self.count += 1
return self.getPersistence(product)
But without knowing how toploader tests your code its hard to
say for sure what's going wrong.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list