Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:23:41+00:00 2026-05-29T06:23:41+00:00

import sys def keepsumming(number): numberlist = [] for digit in str(number): numberlist.append(int(digit)) total =

  • 0
import sys

def keepsumming(number):
    numberlist = []
    for digit in str(number):
        numberlist.append(int(digit))
    total = reduce(add, numberlist)
    if total > 9:
        keepsumming(total)
    if total <= 9:
        return total

def add(x,y):
    return x+y

keepsumming(sys.argv[1])

I want to create a function that adds the individual digits of any number, and to keep summing digits until the result is only one digit. (e.g. 1048576 = 1+0+4+8+5+7+6 = 31 = 3+1 = 4). The function seems to work in some laces but not in others. for example:

$python csp39.py 29

returns None, but:

$python csp39.py 30

returns 3, as it should…

Any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:23:42+00:00Added an answer on May 29, 2026 at 6:23 am

    As others have mentioned, the problem seems to be with the part

    if total > 9:
        keepsumming(total)  # you need return here!
    

    Just for completeness, I want to present you some examples how this task could be solved a bit more elegantly (if you are interested). The first also uses strings:

    while number >= 10:
      number = sum(int(c) for c in str(number))
    

    The second uses modulo so that no string operations are needed at all (which should be quite a lot faster):

    while number >= 10:
      total = 0
      while number:
        number, digit = divmod(number, 10)
        total += digit
      number = total
    

    You can also use an iterator if you want to do different things with the digits:

    def digits(number, base = 10):
      while number:
        yield number % base
        number //= base
    
    number = 12345
    
    # sum digits
    print sum(digits(number))
    # multiply digits
    from operator import mul
    print reduce(mul, digits(number), 1)
    

    This last one is very nice and idiomatic Python, IMHO. You can use it to implement your original function:

    def keepsumming(number, base = 10):
      if number < base:
        return number
      return keepsumming(sum(digits(number, base)), base)
    

    Or iteratively:

    def keepsumming(number, base = 10):
      while number >= base:
        number = sum(digits(number, base))
    

    UPDATE: Thanks to Karl Knechtel for the hint that this actually is a very trivial problem. It can be solved in one line if the underlying mathematics are exploited properly:

    def keepsumming(number, base = 10):
      return 1 + (number - 1) % (b - 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The only nice way I've found is: import sys import os try: os.kill(int(sys.argv[1]), 0)
The python implementation import sys def move(src, dst, tmp, num): if num == 1:
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when
Consider this python program: import sys lc = 0 for line in open(sys.argv[1]): lc
Firstly, here's my script: #!/usr/bin/python import sys, os sys.path.append('/home/username/python') sys.path.append(/home/username/python/flup) sys.path.append(/home/username/python/django) # more path
i have the following script import getopt, sys opts, args = getopt.getopt(sys.argv[1:], h:s) for
cat file_ro.py import sys def file_open(filename): fo=open(filename,'r') fo.seek(7) read_data=fo.read(3) fo.close() print read_data file_open(file.py) But
import sys def a(): print 'aaa' def profiler(frame, event, arg): print event, frame.f_code.co_name, frame.f_lineno,
I've got a piece of code similar to this: import sys def func1(): func2()
import os import sys class Corrector: def correctAll(dir): print ok c = Corrector() c.correctAll(os.getcwd())

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.