Digit sum


I'm following this myself and was hoping someone could answer explain tome how the two parts of the code that turn (n) into a str and then back into integers works out.



well, integers aren't iterable (you can't loop over them). So, you convert to string so you can loop over the numbers, but to do math you need integers, so you convert the individual numbers back to integers so you can add them to total


I used for loop in this case, first converted n into char and then loop through each letter and converting it to a digit while adding up to a variable


can you show the code?


providing full code answers isn't according to the guidelines, please go here, select: make a new topic, fill in the details in the template so we can help you


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.