Python 3.7 Bug

Peter Otten __peter__ at web.de
Mon Mar 25 14:27:14 EDT 2019


Bassam Abdul-Baki wrote:

> Greetings,
> 
> In the following code, there's a bug on certain parameters.
> 
> ----------
> 
> def per(n, steps = 0):
>  digits = [int(i) for i in str(n)]
>  result = 1
>  for j in digits:
>   result *= j
>  steps += 1
>  print(steps, result, sep=" - ")
>  if result == 0:
>   print(result, str(result), len(str(result)), sep=" - ")
>  if len(str(result)) == 1:
>   print(" --- DONE ---")
>   return "DONE"
>  else:
>   per(result, steps)

An indent of four spaces per level would make this much easier to read.

> 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?

If you write a function that does not do what you want it to do -- then, yes 
that is a bug ;)

If I were to guess: You expect that line to return "DONE":

>   per(result, steps)

However, in Python you need to be explicit:

    return per(result, steps)

will return the result of the recursive call.




More information about the Python-list mailing list