My digit_sum works in IDLE, not in interface. Help!


#1

Did I just go about this in a weird way? This is the simplest version my head came up with. Only problem is that when I run through the interface I'm hit with "Your function fails on digit_sum(434). It returns None when it should return 11." yet when I test this code in IDLE, calling digit_sum(434) returns 11 and not None. Where did I go wrong?

def digit_sum(n):
    x = str(n)
    sum_int = 0
    for c in x[:]:
        sum_int += int(c)
    print(sum_int)

#2

When you say "work" you might not mean the same thing as what codecademy is looking for.

Does codecademy say anything about printing? What is being asked for here?


#3

Oh my gosh... I completely overlooked that. Thanks so much.

As a side note, I really liked this exercise. This function is useful for my obsession with the number 3. :sunny:


#4

Just replying to say that I'm obsessed by three, too :smiley:


#5

I wrote this little bit of code as I've been learning as a praise hymn to our holy numeral. I was fascinated by the fact that 3 is the only digit (besides 9) that the sum of digits of any multiplication always yields a multiple of 3.
ex. 123 * 3 = 369. 3 + 6 + 9 = 18. 18 is a multiple of 3.

def SumCon():
    x = int(raw_input("Enter number:"))
    i = int(raw_input("Multiple:"))
    y = x * i
    print(str(x) + " times " +str(i)+ " is " + str(y))
    sum = str(y)
    sum_int = 0
    for c in sum[:]:
        sum_int += int(c)
    print "The sum of the digits " + str(sum) + " is " + str(sum_int)
    g = sum_int / i
    if sum_int % i == 0:
        print str(sum_int) + " is divisible by " + str(i)+":" + " " + str(g) + " times."
    else:
        print str(sum_int) + " is NOT divisible by " + str(i)
    SumCon()

and while sure, there are times when other numbers whose digit sums equal multiples of their numbers, 3 (and 9) are the only ones that do it constantly. They are perfect. I just thought it was cool to be able to see it. Python is awesome!


#6

Not actually sure if I should continue the discussion here (since we should talk about python problems), however my guess is that the 9 magic divisibility rule is due to the fact that we use a 10-base numerical system; basically, every time we use a n-base we obtain a (n-1)-magic divisibility rule. I don't have the mathematical knowledge to prove this but on the empirical level it seemed to work.
However since this is all about coding, later I'll try to fork your code to better explain my idea.


#7

It took more lines than expected, so I decided to link the page of code where I wrote it (i used another site cause I couldn't make codecademy labs work):
https://trinket.io/python/52b034a66d
This is an example of how it should work.


It's probably a huge chunk of useless code, however it was a nice exercise and Python is awesome for real :grinning:


#8

numpy is not part of the standard library so it may not be installed everywhere, but it is a common package.

Installation is normally done with:

$ pip install numpy

And this library has a function for representing ints in bases up to and including 36:

>>> from numpy import base_repr
>>> print base_repr(57, 31)
1Q

Which can then be reversed by the int class:

>>> int('1Q', 31)
57

Digit sum: using arithmetic's is faster, but a simple way to sum digits is otherwise:

>>> print sum(map(int, str(3587)))
23

(Convert to string and iterate through the characters, converting each to int. I got a bit fancy about how I did this, could just as well be a simple loop)

Or with another base:

>>> from numpy import base_repr
>>> base = 31
>>> print sum(int(digit, base) for digit in base_repr(3587, base))
47

#9

Good, thank you I tried to look for something similar but didn't get any result. Maybe I'll try to see if my idea works for other bases.