python - Multiple IF Statements - Code Stops After IF Condition Not Met -


i working on python on coding bat. problem having issues with: http://codingbat.com/prob/p107863. not sure if problem them. here code. used add of inputs not equal 13. there better way it. however, want know how evaluate next if statement if previous if statement's condition not met.

def lucky_sum(a, b, c):   results=0   if != 13:      results= results +   if b != 13:      results= results + b   if c != 13:      results= results + c   return results  #edit: indented 2 spaces correct op formatting error -sequoia 

if run lucky_sum(1, 13, 3) returns 1 instead of 4. sure easy fix.

a better way write function use non-keyword arguments. , use sum() function on generator expression return sum.

>>> def lucky_sum(*nkwargs): ...     return sum(x x in nkwargs if x != 13)  >>> lucky_sum(1, 13, 3) 4 

in original code, there issue of indentation error. return statement not indented.


as per modification, above code, , 1 in question won't work coding bat problem. there need stop adding once find value equal 13.

for can modify original function return sum if block, value 13. i'll give corresponding modification function here:

>>> def lucky_sum(*nkwargs): ...     try: ...         index = nkwargs.index(13) ...         return sum(nkwargs[:index]) ...     except valueerror: ...         return sum(nkwargs)  >>> lucky_sum(1, 3, 4) 8 >>> lucky_sum(1, 3, 13,  4) 4 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -