-1

Putting an if-elif-else statement on one line? did not give me enough information figure out my problem. I want to turn

if pointer == 0:
    quit(print("ERROR:POINTER VALUE < 0"))
else:
    pointer -=1

into a one line if statement so I can put it in an exec() statement. Please elaborate on the info provided in the linked question

2 Answers2

0

exec doesn't actually need its argument to be in one line. You can pass it a multi-line string.

exec('if a:\n print("true")\nelse:\n print("false")')
Nick Frost
  • 490
  • 2
  • 12
0

You can use the ternary operator.

Here is a better explanation - Does Python have a ternary conditional operator?

Read up on the wikipedia entry here - https://en.wikipedia.org/wiki/%3F:

Basically, it's a way to write a conditional when you have only two possible results for boolean comparison. It's great for very short lines.

For example:

x = 5 if True else x = 4
x = 1 if x == 0 else y = 10/x

In your case, it may not be the best decision, as your line will grow pretty long.

blake
  • 76
  • 1
  • 4