getting the sum of digits in a number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    getting the sum of digits in a number

    i made it like this:
    Code:
    def tvarsumman(n):
        if n<10:
            print n
        else:
            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
    Last edited by bartonc; Oct 26 '07, 04:55 AM.
  • DDCane
    New Member
    • Oct 2007
    • 16

    #2
    temp

    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 edited by bartonc; Oct 26 '07, 04:55 AM.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by DDCane
      <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?):[CODE=python]
      >>> user_input = '12345'
      >>> numList = list(user_input )
      >>> total = 0
      >>> for num in numList:
      ... try: # prevent errors in the input from breaking the loop
      ... total += int(num)
      ... except (ValueError, TypeError):
      ... pass # do nothing
      ...
      >>> print total
      15
      >>> [/CODE]

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by DDCane
        i made it like this:
        Code:
        def tvarsumman(n):
            if n<10:
                print n
            else:
                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.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bartonc
          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:[CODE=python]
          >>> def SumDigits(n):
          ... numList = list(str(n))
          ... total = sum(int(c) for c in numList)
          ... print total
          ...
          >>> SumDigits(99)
          18
          >>> [/CODE]
          Last edited by bartonc; Oct 26 '07, 04:57 AM.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by DDCane
            i made it like this:
            Code:
            def tvarsumman(n):
                if n<10:
                    print n
                else:
                    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
            [CODE=python]
            >>> def tvarsumman(n):
            ... total = 0
            ... while n > 0:
            ... total += n%10
            ... n /= 10
            ... return total
            ...
            >>> print tvarsumman(99)
            18
            >>> print tvarsumman(123)
            6
            >>> [/CODE]
            Last edited by bartonc; Oct 26 '07, 05:47 AM.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by DDCane
              i made it like this:
              Code:
              def tvarsumman(n):
                  if n<10:
                      print n
                  else:
                      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.
              [code=Python]def tvarsumman(n):
              if n < 10:
              return n
              else:
              n, rem = divmod(n, 10)
              return tvarsumman(n) + rem[/code]

              Example:

              >>> tvarsumman(99)
              18
              >>> tvarsumman(1234 5)
              15
              >>> tvarsumman(9876 54321)
              45
              >>> tvarsumman(1013 5)
              10
              >>>

              Comment

              • Smygis
                New Member
                • Jun 2007
                • 126

                #8
                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:
                [code=python]
                def numsum(n):
                if n > 0:
                n = n % 10 + numsum(n/10)
                return n
                [/code]

                Something like that?

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by Smygis
                  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)

                  Comment

                  • Smygis
                    New Member
                    • Jun 2007
                    • 126

                    #10
                    Originally posted by bartonc
                    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.

                    Comment

                    • DDCane
                      New Member
                      • Oct 2007
                      • 16

                      #11
                      Originally posted by Smygis
                      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?

                      Comment

                      • Smygis
                        New Member
                        • Jun 2007
                        • 126

                        #12
                        Originally posted by DDCane
                        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.

                        Comment

                        Working...