I have an exercise from a book with this code snippet:
def binomial_coeff(n, k):
"""Compute the binomial coefficient "n choose k".
n: number of trials
k: number of successes
returns: int
"""
if k == 0:
return 1
if n == 0:
return 0
res = binomial_coeff(n-1, k) + binomial_coeff(n-1, k-1)
return res
the goal of the exercise is to rewrite the if statements as nested conditional expressions. I understand how to write a conditional expression e.g.
return 1 if k == 0
What am I missing here? By doing this nested, I can't seem to figure it out. PyCharm keeps complaining about that it the second part of the code is unreachable.
return 1 if k == 0 else return 0 if n == 0
if-elif-elseconditional expressions and your doubt about Pycharm complaining, is that the error means any statement after areturnis unreachable in a functionreturns on the same line is actually invalid syntax, so IDK why you're getting a warning about unreachable code.