469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

getting the sum of digits in a number

16
i made it like this:
Expand|Select|Wrap|Line Numbers
  1. def tvarsumman(n):
  2.     if n<10:
  3.         print n
  4.     else:
  5.         tvarsumman((n/10) + n%10)
but when it comes to 99 it does this

99--> 9+9=18-->1+8=9

thats wrong according to my professor.

can u help fix my function?

and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
Oct 25 '07 #1
11 15095
DDCane
16
one other small problem i have is i need to define a function that takes the number i put in splits it up and adds them together for example:

123 --> 1+2+3=6
88 --> 8+8=16

thanks for all ur help guys u are really saving my life!
Oct 25 '07 #2
bartonc
6,596 Expert 4TB
<snip>and one other small problem i have is i need to define a function that takes the number i put in splits it up and adds them together for example:

123 --> 1+2+3=6
88 --> 8+8=16

thanks for all ur help guys u are really saving my life!
Last things first (by the way, where my dict functions helpful?):
Expand|Select|Wrap|Line Numbers
  1. >>> user_input = '12345'
  2. >>> numList = list(user_input)
  3. >>> total = 0
  4. >>> for num in numList:
  5. ...     try:  # prevent errors in the input from breaking the loop
  6. ...         total += int(num)
  7. ...     except (ValueError, TypeError):
  8. ...         pass  # do nothing
  9. ...     
  10. >>> print total
  11. 15
  12. >>> 
Oct 25 '07 #3
bartonc
6,596 Expert 4TB
i made it like this:
Expand|Select|Wrap|Line Numbers
  1. def tvarsumman(n):
  2.     if n<10:
  3.         print n
  4.     else:
  5.         tvarsumman((n/10) + n%10)
but when it comes to 99 it does this

99--> 9+9=18-->1+8=9

thats wrong according to my professor.

can u help fix my function?

and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
You are welcome. I would not have guessed that English was not your first language. However, I am having some trouble following this.
Oct 26 '07 #4
bartonc
6,596 Expert 4TB
You are welcome. I would not have guessed that English was not your first language. However, I am having some trouble following this.
If recursion is not a requirement, then this may be what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. >>> def SumDigits(n):
  2. ...     numList = list(str(n))
  3. ...     total = sum(int(c) for c in numList)
  4. ...     print total
  5. ...     
  6. >>> SumDigits(99)
  7. 18
  8. >>> 
Oct 26 '07 #5
bartonc
6,596 Expert 4TB
i made it like this:
Expand|Select|Wrap|Line Numbers
  1. def tvarsumman(n):
  2.     if n<10:
  3.         print n
  4.     else:
  5.         tvarsumman((n/10) + n%10)
but when it comes to 99 it does this

99--> 9+9=18-->1+8=9

thats wrong according to my professor.

can u help fix my function?

and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
Expand|Select|Wrap|Line Numbers
  1. >>> def tvarsumman(n):
  2. ...     total = 0
  3. ...     while n > 0:
  4. ...         total += n%10
  5. ...         n /= 10
  6. ...     return total
  7. ...     
  8. >>> print tvarsumman(99)
  9. 18
  10. >>> print tvarsumman(123)
  11. 6
  12. >>> 
Oct 26 '07 #6
bvdet
2,851 Expert Mod 2GB
i made it like this:
Expand|Select|Wrap|Line Numbers
  1. def tvarsumman(n):
  2.     if n<10:
  3.         print n
  4.     else:
  5.         tvarsumman((n/10) + n%10)
but when it comes to 99 it does this

99--> 9+9=18-->1+8=9

thats wrong according to my professor.

can u help fix my function?

and all the ones u have made so far have been perfect! u are really helping me. thank u sir! :P
Using recursion, you need a way to terminate the function. You can do that by substituting 'print n' with 'return n'. The last line also needs a return, and n%10 needs to be outside of the argument list. Use divmod() to determine the integer quotient and remainder.
Expand|Select|Wrap|Line Numbers
  1. def tvarsumman(n):
  2.     if n < 10:
  3.         return n
  4.     else:
  5.         n, rem = divmod(n, 10)
  6.         return tvarsumman(n) + rem
Example:

>>> tvarsumman(99)
18
>>> tvarsumman(12345)
15
>>> tvarsumman(987654321)
45
>>> tvarsumman(10135)
10
>>>
Oct 26 '07 #7
Smygis
126 100+
Tjo! Gissar att du är svensk ;)
Och att du ska räkna ut tvärsumman med hjälp av en rekursiv funktion.

(Sorry about the language everyone else. And DDCane to if im wrong about his nationality, nothing realy important above)

Anyway:
Expand|Select|Wrap|Line Numbers
  1. def numsum(n):
  2.     if n > 0:
  3.         n = n % 10 + numsum(n/10)
  4.     return n
  5.  
Something like that?
Oct 26 '07 #8
bartonc
6,596 Expert 4TB
Tjo! Gissar att du är svensk ;)
Och att du ska räkna ut tvärsumman med hjälp av en rekursiv funktion.
Two Swedes on the boards! Very cool. My father was half Swedish, but I never learned the language.

(My apologies if I have mistaken your nationality)
Oct 26 '07 #9
Smygis
126 100+
Two Swedes on the boards! Very cool. My father was half Swedish, but I never learned the language.

(My apologies if I have mistaken your nationality)

Nope, Youre entirely correct. I'm from Sweden.
I klicked report insted of reply :/ meh.
Oct 26 '07 #10
DDCane
16
Nope, Youre entirely correct. I'm from Sweden.
I klicked report insted of reply :/ meh.
Hey guys! Actually I'm American but I live in Sweden at the moment. Where in Sweden are from?
Oct 27 '07 #11
Smygis
126 100+
Hey guys! Actually I'm American but I live in Sweden at the moment. Where in Sweden are from?
Up in the north. Live somewhere in the area around Umeå now.
Oct 27 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by ssmith579 | last post: by
4 posts views Thread by Shahar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.