Thanks, guys - got it! I was suspecting that my solution is too complex and that there must be a simpler way to convert integers into a digit sum. Have a great morning/day/evening, Raf
On Mon, Dec 9, 2013 at 9:23 AM, Amit Saha <amitsaha...@gmail.com> wrote: > On Mon, Dec 9, 2013 at 6:08 PM, Rafael Knuth <rafael.kn...@gmail.com> wrote: >> Hej there, >> >> I wrote a program that converts an integer into a digit sum: >> >> def DigitSum(YourNumber): >> DigitList = [] >> YourNumber = str(YourNumber) >> for i in YourNumber: >> DigitList.append(int(i)) >> print(sum(DigitList)) >> >> DigitSum(55) >> >>>>> >> 10 >> >> It actually works but I was wondering if that's the only way to solve >> the task of converting an integer into a digit sum? I learned from >> past conversations on this mailing list that often times there is a >> better, more elegant and shorter way to write a program, and I was >> wondering if that's the case here. > >>>> sum([int(digit) for digit in str(55)]) > 10 > > That's using list comprehensions. Since a string is basically a > sequence, you can do the above. > > Best, > Amit. > > -- > http://echorand.me _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor