Sum of Integers - 'int' object is not iterable

前端 未结 5 1182
盖世英雄少女心
盖世英雄少女心 2020-11-30 14:54

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:
               


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 15:18

    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
    

提交回复
热议问题