Sum of Integers - 'int' object is not iterable

前端 未结 5 1184
盖世英雄少女心
盖世英雄少女心 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条回答
  •  囚心锁ツ
    2020-11-30 15:16

    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.

提交回复
热议问题