Your C code doesn't contain two return statements. Neither should your python code... The translation of your ternary expression is n if n<m else m, so just use that expression when you return the value:
def minn(n,m):
return n if n<m else m
Answer from Karoly Horvath on Stack OverflowYour C code doesn't contain two return statements. Neither should your python code... The translation of your ternary expression is n if n<m else m, so just use that expression when you return the value:
def minn(n,m):
return n if n<m else m
def minn(n,m):
return n if n<m else m
The expr1 if expr2 else expr3 expression is an expression, not a statement. return is a statement (See this question)
Because expressions cannot contain statements, your code fails.
I think the ternary operator would be more readable if the order was reversed
Conditional Expresion/Statements - Ideas - Discussions on Python.org
Is using the ternary operator bad practice?
Python's Boolean operators do not return Boolean
Videos
So currently all implementations of the ternary operator go something like this:
a = (Condition) ? b : c
However I feel like that quite often I just want to see the possible outcomes first and then look at the condition. I feel like when the conditions get a bit longer it quickly becomes hard to read. The outcomes are almost always simple because I wouldn't use a ternary operator if that wasn't the case.
I feel like this format would be more readable in 90% of cases I encounter:
a = b : c ? (Condition)
Potential downside is that it is a bit more removed from the logical execution order perhaps.
It might be a small thing but I feel like I would use the ternary operator more if it was easier to read at a glance.
What do you think?