4/15 & 5/15 help?


#1

I've been following this & understand this section well but it's like i have absolutely no idea about 4 & 5. I've read answers on the forum & have no clue how people got them. Is there something i should read up on or restart? Any help would be appreciated.


#2

I'm kinda in the same boat, idk why I was doing fine until this point


#5

Here was my solution:

> def digit_sum (n):
>         # Setting a counter
>         count = 0
>         #  While Loop to go though digits until 0
>         while n > 0:
>            # Getting the far right digit
>             x = n % 10
>            # Adding digit to our count
>             count += x
>            # Removing far right digit
>             n = n // 10
>         # Returning the total count
>         return count

#6

Hi @explvcit,

I wouldn't be as bold to question your math competence, but I think it's fair if I can grant some direction towards the solution. The Python docs show a very simple option using math.factorial (max 4 lines of code will do the job). Read and try it...don't forget to import math though :-).

Let's help each other!

Cheers


#7

Forgot to mention Solution for question 5:

def factorial (x):
    total = 1
    while x > 1:
        total = total * x
        x = x - 1
    return total

#8

@khoel
I think the point of the task is to create your own function
rather than to import a function from math that does the job :wink:


#9

Hi @marmaa,

Hmm, in this specific segment of the course, I would think that's left to your specific interpretation of that very task. Thanks for chipping in :smile:

Regards