1

Quite new to the realm of coding and just trying to improve a bit every day. Trying to understand exactly what's happening with this line of code:

sum(int(x) for x in str(n)) 

Within this function:

### digit sum

def digital_root(n):
    while n>9:
        n = sum(int(x) for x in str(n))
    return n

print(digital_root(n))

Basically, it looks to me like the line of code is saying to convert n into a string and then x somehow represents each individual integer within the "string" and then the sum function...well, sums them?

I feel like that's not 100% accurate though for some reason. Would appreciate any clarification!

Kyle
  • 61
  • 5
  • 2
    Yup. thats what it does. keeps doing that till it comes to a single digit. 1234->10->1. – lllrnr101 Apr 03 '21 at 17:48
  • 2
    You can have a look at https://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it for explanations about list comprehensions (what you have here is actually a generator expression, but they have many common points.) – Thierry Lathuille Apr 03 '21 at 17:50

3 Answers3

1

The spotlight here is (int(x) for x in str(n)) where str(n) converts n into string, for x in str(n) iterates over each character of newly generated string with variable x, int(x) converts each character x into integer and (int(x) for x in str(n)) in whole this line generates single integer of each character in n. (like for 978, it will generate 9,7,8). n = sum(int(x) for x in str(n)) sums them up and store it in n. now n for 978 will be 9+7+8 = 24.

By the way you should remove while loop. it is doing nothing because int(x) for x in str(n) is self iterating.

you should use

def digital_root(n):
    n = sum(int(x) for x in str(n))
    return n

print(digital_root(8))
0

Here is the code broken down.

def digital_root(number):
    #make sure that the number is above 9
    while number>9:
        #create a list where every digit in the number given will go
        digitsList = []
        #iterate through the number give
        #Note: you cannot iterate through a integer so we make it a string
        for digits in str(number):
            #put each digit in that number to digitsList.  
            #Note: later we will find the sum of the list using a built in python function
            #Python cannot find the sum of a list containing string. So we have to append
            #integers the the list
            digitsList.append(int(digits))
        #find the sum of the list
        number = sum(digitsList)
    #return it
    return number

number = 14
print(digital_root(number))
BuddyBob
  • 4,024
  • 1
  • 6
  • 29
-1

Let me try to explain if your value of n is 111 and you convert it to string. Once you loop it like

for i in str(111):
   print(i)

Then the output will be

    1
    1
    1


so when you sum the output, the answer will be 3.

def digital_root(n):
    while n>9:
        n = sum(int(x) for x in str(n))
    return n

print(digital_root(111))

Output: 3

Talha Anwar
  • 1,230
  • 7
  • 19
  • sorry for the downvote, must have clicked by mistake. cant change it anymore, but if you edit it in anyway and let me know I can upvote to fix it. – JL Peyret Apr 03 '21 at 19:11