mko - 1 year ago 45

Python Question

`def sum10(a, b):`

if sum([a, b]) % 10 == 0: return True; return False

print sum10(7, 3)

print sum10(-13, -17)

print sum10(3, 8)

the result is:

`True`

True

None

not what I expected:

`True`

True

False

any idea?

Answer Source

Your code

```
def sum10(a, b):
if sum([a, b]) % 10 == 0: return True; return False
```

is equivalent to

```
def sum10(a, b):
if sum([a, b]) % 10 == 0:
return True; return False
```

so `return False`

is never evaluated.

Some (of the probably endless) alternatives:

```
if sum([a, b]) % 10 == 0:
return True
return False
```

or

```
return sum([a, b]) % 10 == 0
```

or

```
return True if sum([a, b]) % 10 == 0 else False
```

or

```
return False if (a+b) % 10 else True
```

or (the most readable IMHO)

```
return not (a + b) % 10
```