Bassam,
Greetings.
On 26/03/19 1:14 AM, 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)
----------
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?
No - the Python 3.7 does exactly what you've asked it to do!
0
What is the objective of this piece of code? Other than "CORRECT!" and
"WRONG!" there are few hints, especially the function name: "per". What
does "per" mean - not everyone is a mathematician.
1
Had we been shown two entire output reports, eg one for 20 (correct) and
another for 25 (incorrect), the answer would have been immediately apparent.
2
Is it a good idea/best practice to sometimes code the two values as
"results" followed by "steps", and sometimes "steps" followed by
"results"; or does this invite confusion?
3
Please express "len(str(result)) == 1" in English (or your preferred
spoken-language). Is there an easier way to find numbers expressed in
multiple digits? (and that's another overly-complicated way to say the
same thing!)
4
If a 'debug print' is added at the beginning of the function, is the
function being "called" more times than expected? Should/could more
debug-print statements be added to check other sub-calculations along
the way?
(PS 'debug-print' added to your code for debugging/testing and then
removed for 'production-use' is a quick-and-dirty tactic. Please add a
note to your Python ToDo list: "learn to use logging library instead of
debug-print", otherwise wiser heads will criticise this advice, which
works well when learning but perhaps not at the professional level)
5
Humility: that it might be a bug in your code rather than in Python 3.7.
Respect: It would be polite to mention if this is a student/homework
assignment - or the text that you are following for self-improvement.
Charity begins at home: That people here will volunteer to help you, and
can be more helpful if you help us - particularly by providing
information which will save our (valuable) time.
Here's hoping you are now equipped to find the source of the problem...
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list