0

I'm doing python tutorials on pyschools.com, and to answer the above question, my method is converting each integer to a string, reversing the order of the characters, then converting it back to an integer, dividing it by 10, and adding the remainders. This is probably a very muddled way to accomplish this, but it's the only way I can think of.

My code is as follows:`

def getSumOfFirstDigit(num): 
    answer=0
    for number in num:
        str(number)
        number[::-1]
        int(number)
        t = number%10
        answer=answer+t
    return answer`

When I run it with the example: getSumOfFirstDigit([12, 23, 34, 45, 56]), it returns the error:

Traceback (most recent call last):
File "Code", line 5, in getSumOfFirstDigit
TypeError: 'int' object has no attribute '__getitem__'`

I thought the "str(number)" would take care of what the error says.

8 Answers 8

3

Use sum, list slicing and a generator expression:

>>> sum(int(str(n)[0]) for n in [1, 23, 45, 234])
9
2
  • What is the purpose of the int() function in your solution? I under stand that the rest is for the sum of the first char in the converted string for each one, but that would take away the need for the int() portion wouldn't it? Commented Sep 11, 2013 at 15:20
  • @user2769237 Because sum(['1','2','4','2']) doesn't work. sum can only operate on numbers.
    – Brian
    Commented Sep 11, 2013 at 15:25
2

Here is a different approach: you can get the first digit by repeatedly divide the number by ten until it is less than ten:

def first_digit(n):
    while n > 9:
        n /= 10
    return n

After that, it is just a matter of calling the sum() function to do the job:

def sum_of_first_digits(seq):
    return sum(first_digit(n) for n in seq)

Discussion

  • Assumption: all numbers are zero or positive, there is no check for negative or non-number input
  • If you are not allowed to use the sum() function, write a loop
1

Of course it will, but you need to use it somehow.

Try this:

answer = answer + int(str(number)[0])

or instead of doing answer = answer + something, you can do:

answer += int(str(number)[0])

1

The reason you're getting that error is because you've written:

    str(number)

and later on:

    int(number)

but not assigned them to number, so it's still an int.

Replace those with:

    number=str(number)
    number=int(number)

and you should be fine.

1

It is telling you TypeError: 'int' object has no attribute 'getitem' because you are treating the variable "number" as if it is a list. It is not. It is consistence of the numbers within num which are integers. So "number[::-1]" is a no no. One way to do it is....

    def getSumOfFirstDigit(num): 
        answer=[]
        for number in num:
            number=str(number)
            number=number[0]
            answer.append(int(number))
        return sum(answer)
1
def getSumOfFirstDigit(num):
    sum1 = 0
    for x in num:
        temp = str(x)
        sum1 += int(temp[0])
    return sum1
0
0

This should work:

def getSumOfFirstDigit(num):
   sum = 0
   num_strings = map(str, num)
   for num in num_strings:
       sum += int(i[0])
   print (sum)

The problems with your code:

def getSumOfFirstDigit(num): 
    answer=0
    for number in num:
        str(number)           # you are not assigning the string to anything. Maybe you need to store it in a temporary variable
        number[::-1]          # number is an int here so you can't do this
        int(number)           # number is already an int
        t = number%10
        answer=answer+t
    return answer

And your code should be something like this (if I am correctly understanding what you want to do):

def getSumOfFirstDigit(num): 
    answer=0
    for number in num:
        temp = str(number)
        temp = [::-1]
        temp = int(number)
        t = temp%10
        answer += t
    return answer
0
def getSumOfFirstDigit(numList): 
    total=0
    for item in numList:
        newItem=str(item)
        length=newItem[len(newItem)-len(newItem)]
        total+=int(length)
    return total
1
  • Your code-only answer would benefit from having an explanation of the problem it addresses.
    – addiedx44
    Commented Jan 29, 2014 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.