Python 3.7 Bug

Chris Angelico rosuav at gmail.com
Mon Mar 25 14:21:25 EDT 2019


On Tue, Mar 26, 2019 at 5:08 AM Bassam Abdul-Baki <babdulbaki at gmail.com> wrote:
>
> def per(n, steps = 0):
>  if len(str(result)) == 1:
>   print(" --- DONE ---")
>   return "DONE"
>  else:
>   per(result, steps)
>
> ----------
>
> What the program does:
> If I run per(X) and X is a multiple of 10, I should end up with 0 in a
> finite amount of steps.
>
> The problem:
> If I run per(54), I do not get 'DONE' printed through the return
> statement.  WRONG!
>
> If I run per(20), I do get 'DONE' printed through the return statement.
> CORRECT!
>
> 20, 30, etc. are correct.  25, 45, etc. are not.
>
> Is this a bug?

Yes, it is - not a bug in Python, but a bug in the above code :)

I would recommend exploring the part of the code that I quoted above,
and researching how recursion works, and specifically what 'return'
does. That should put you on the right path to figuring this out.

ChrisA



More information about the Python-list mailing list