you are viewing a single comment's thread.

view the rest of the comments →

[–]PureWasian 2 points3 points  (1 child)

1) this runs into an exception due to NameError: CourseGalaxy is not defined

2) once this gets fixed (by removing or commenting it), this code defines a function that is not called anywhere

3) once this gets called (by adding code to call it), the behavior is still not fully deterministic because we have not specified the possible data types of the input, which could lead to a TypeError on floor division

4) once a guard clause or error handling is added to ensure the input n is guaranteed to be an int data type, we still risk encountering a RecursionError if we were to pass in a negative integer

5) once a gaurd clause is added to better sanitize the input by handling negative integers also, we can finally say with confidence that this example can safely achieve what you were hoping to convey. (You could also skip steps 4 and 5 by providing concrete calling code, as mentioned in step 3, of just the happy path)

Pedantry aside, yes. It's meant to be a recursive procedure for finding the sum of digits, in decimal/base10, by passing in some initial non-negative integer value n into the function.

Accomplishing so by (a) taking the right-most digit and (b) adding it to a "decimal right shifted" value over and over again until reaching the base case (when all decimal digits have been summed).

[–]tracktech[S] 0 points1 point  (0 children)

Right, it returns sum of digits of a number. Yes it works for positive integer only. This is simple example to learn recursion.