I am learning python, and trying to use some ternary operators.
I am trying to make the function below using a ternary:
def array_count9(nums):
count = 0
for i in nums:
if i == 9:
count += 1
return count
I have tried:
def array_count9(nums):
count = 0
count += 1 if i == 9 for i in nums else pass
return count
which threw a SyntaxError
, then after looking around I found this and changed my code for what I believed was better ordering:
def array_count9(nums):
count = 0
count += 1 if i == 9 else pass for i in nums
return count
Still receiving SyntaxError
which is pointing at the for
. I've tried using parentheses in different places too.
I have looked around and there are other related threads such as this and this, which resulted in me trying this:
def array_count9(nums):
count = 0
count += 1 if i == 9 else count == count for i in nums
return count
I've also tried other resources by searching Google, but I can't quite work it out. Please teach me.
Thanks