Digit_sum : my code works but,

This is my code :

def digit_sum(number):
total=0
for x in range(len(number)):
total=total+int(number[int(x)])
return total
number=raw_input(“Please enter a digit so we can add as per Instructions :”)
print(digit_sum(number))

i) which works, produces the correct answers for all integers entered, like 0,11,123,999999, however there is an "error at the bottom
-> Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a “object of type ‘int’ has no len()” error.
ii) I have tried it in an "open environment like pythonanywhere dot com … no issues.
*
Can someone explain to me what the compiler/interpreter is trying to tell me ?
Thanks.
Marc

REF : https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/digitsum?

That is the line throwing the error. An integer is not iterable so has no length property.

Sorry MTF,
Still don’t get it :
i) number is a string (4 "characters’ in length).
ii) len(string) = 4 … as per the example we were given, let’s say 4321
iii) thus it becomes : for x in range(4):
-> what is wrong with that ?
Thanks,
Marc

REF: https://www.pythoncentral.io/pythons-range-function-explained/

1 Like

Not as such. Integers have binary equivalents where strings do not. To treat numbers as strings we need to convert them to character data.

str(number)

then they become iterable.

total = 0
for x in str(number):
    total += int(x)

Hello MTF,
I appreciate your time & effort.
From my code, I had my number {variable} taken from raw_input, making it a string.
-> why would I need to force it back to string again with numer=str(number ?
Thanks,
Marc

One would never assume that inputs are derived from user input. By the variable name used in the parameter the safer assumption would be that number is in fact a number. User input needs to be vetted, validated and cast to the correct data type before calling a computational function with that value.

It won’t cause any damage to convert a string to a string, but the code will throw an error if we pass in an actual number and then do not convert it. You’ll want the function to work when number literals are passed in. That’s what the SCT will be doing. User input is not expected in the exercise.

OK - that’s where I missed : user input was not expected - I did it for myself to able to test faster.
Excellent. Thanks !
Marc

1 Like

I think you can convert your user input directly form:
number=int(input(“Please enter a digit so we can add as per Instructions :”))

or after the user input and convert it like this:
number = int(number)

and also inside your function you can retype you total=total +int(number[int(x)]):
total = total + x or total += x
NOTE: Indentation inside your function does matter for the code to run perfectly
I think I fix your code error

Thanks Alex.
My initial question was due to the “error” it generated … but on the other hand it always worked great with all I threw at it. I also made it work in a plain bash console that threw no errors whatsoever.
-> that’s why I could figure out why it was within the “interpreter” environment.
Have a great day.
Marc