Python Forum
how to return False in ternary condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to return False in ternary condition
#1
The code below is running inside a while True loop
if play=='y':
    print('ok')
else:
    return False
I would like to write those lines in a ternary form.
Problem with my code is it seems you cannot return in ternary conditions.

print('ok') if play=='y' else return False
Is there a way to achieve this?
Reply
#2
Other than for style, why do you want to do that?
Reply
#3
You can achieve this with semicolons. You dont ordinarily see ; in python, but you can use them.

print("hi");print("hi again")
buran write Dec-10-2020, 05:08 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#4
The ternary syntax is a special form of an expression, and is limited to expressions only. It isn't a full control flow like a real "if" statement. You can't put statements like "return" into those expressions.
KEYS likes this post
Reply
#5
Semicolons just make linters angry. One statement per line please. Why do people like to write dense, unreadable code?
buran likes this post
Reply
#6
return statement can only be used in a function, not elsewhere
Reply
#7
Quote:return statement can only be used in a function, not elsewhere
This may or may not be a problem here. We have no context provided for the code in question. But this is invalid Python if it is in a function or not. "return" is not an expression. A ternary expression is "expression condition expression". In
answer = 'odd' if x % 2 else 'even'
'even' and 'odd' are expressions and 'if x % 2 else' is the condition.

An expression is something that evaluates to a value. A string is an expression. An equation is an expression. A function call is an expression. A "return", ironically, is not an expression because it does not evaluate to a value.
Reply
#8
Some additional tidbits. In Python there is conditional expression set forth in PEP308.

Guido in Python-Dev list:

Quote:Pleas stop calling it 'ternary expression'. That doesn't explain
what it means. It's as if we were to refer to the + operator as
'binary expression'.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 895 Jan-21-2023, 05:01 PM
Last Post: nicholas
  else condition not called when if condition is false Sandz1286 10 5,736 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,698 Jun-01-2020, 06:00 PM
Last Post: penahuse
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,186 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Why does this return False? gunitinug 1 1,849 Jul-13-2019, 07:11 AM
Last Post: perfringo
  ternary operator with unpacking joaomcteixeira 5 3,840 Jan-08-2019, 08:49 AM
Last Post: joaomcteixeira

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020