Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Using Nested Conditional Expressions

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

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Mar 21, 2022 at 4:16

lang-py

AltStyle によって変換されたページ (->オリジナル) /