Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of Integers - 'int' object is not iterable

Tags:

python

My question is related to the sum function in python.

So my code is

def black_jack(a, b):
    if sum(a, b) > 21:
        return 0
    else:
        return sum(a, b)


print black_jack(10, 5)

I get an error reading:

'int' object is not iterable

Can someone explain why this happens and how to fix it?

like image 338
Ogough Avatar asked Nov 18 '15 20:11

Ogough


People also ask

How do I fix int object is not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

Why is int not iterable Python?

Conclusion # The Python "TypeError: 'int' object is not iterable" occurs when we try to iterate over an integer or pass an integer to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(10): .

How do I make an INT iterable?

Iterators and Iterator Protocol So, in a gist, __iter__ is something that makes any python object iterable; hence to make integers iterable we need to have __iter__ function set for integers.

How do I fix TypeError type object is not iterable?

The Python "TypeError: 'type' object is not iterable" occurs when we try to iterate over a class that is not iterable, e.g. forget to call the range() function. To solve the error, make the class iterable by implementing the __iter__() method.


3 Answers

sum is builtin function, look at the documentation:

In [1]: sum?
Docstring:
sum(sequence[, start]) -> value

Return the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0).  When the sequence is
empty, return start.
Type:      builtin_function_or_method

so you need to pass it a iterable! :
solution1

sum([a, b]) #list

solution2

sum((a, b)) #tuple
like image 39
Iman Mirzadeh Avatar answered Oct 12 '22 17:10

Iman Mirzadeh


What's wrong with just the following?

def black_jack(a, b):
    if a + b > 21:
        return 0
    else:
        return a + b

print black_jack(10, 5)

In Blackjack, one can have much more than just two cards, but with your example, it appears that you assume that a hand can have only two cards. If you allow for a variable number of cards, then you'd need to use an iterable object as others have suggested:

def black_jack(values):
    total = sum(values)
    return 0 if total > 21 else total

print black_jack(10, 5)

From the documentation for sum():

sum(iterable[,start])

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable's items are normally numbers, and the start value is not allowed to be a string.

For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain().

New in version 2.3.

like image 35
Tyler Crompton Avatar answered Oct 12 '22 17:10

Tyler Crompton


Look at the documentation:

sum(iterable[, start])

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.

So you have to pass an iterable as argument, not an int!

sum((a, b)) should work correctly.

This is a function which is intended to be used when you have many values stored in a list (for example), if you want to sum two values, you should simply use a + b.

like image 90
Delgan Avatar answered Oct 12 '22 16:10

Delgan