I was dealing with CodingBat problems to learn better and more efficiently and the problem I've faced was:
Given 2 int values, return True if one is negative and one is positive. Except if the parameter "negative" is True, then return True only if both are negative.
pos_neg(1, -1, False) → True
pos_neg(-1, 1, False) → True
pos_neg(-4, -5, True) → True
I wrote that code to run the desired process
def pos_neg(a, b, negative):
if (a<0 and b>0) or (a>0 and b<0):
return True
elif negative and (a<0 and b<0)
return True
else:
return False
but I get
Compile problems:
invalid syntax (line 4)
as an Error. Solution given by CondaBat is:
def pos_neg(a, b, negative):
if negative:
return (a < 0 and b < 0)
else:
return ((a < 0 and b > 0) or (a > 0 and b < 0))
I see that the example code given by is faster and more efficent compared to mine but I can't see why my elif statement returns an error.
Aucun commentaire:
Enregistrer un commentaire