I have a question along related lines, but not identical, to Can we have proper formatting for console output? and Should I prefix example Python code with ">>>" to indicate running in the interpreter? . My question is specifically about interspersing code and its console output, in a way that could generalize to longer code segments.
Background
Often I want to show both console input and output in a Stack Overflow question or answer. I'm looking for a markdown strategy or convention that is easy to type, clear and unambiguous.
Here's a Python example. Let's say that Alice does it this way:
>>> a = first_stage()
>>> print(a)
1.234
>>> b = next_stage(a)
Hopefully it's obvious that the >>>
is the Python prompt, that anything following it is what you're supposed to type at the Python prompt, and that the lines without >>>
are what you should consequently see as console output. It's often useful to show intermediate outputs like this, to help in understanding the flow of a question or answer. This convention works OK but of course it makes it annoying to copy, paste and try the code (the >>>
have to be deleted by hand), and sometimes it's helpful to intersperse console output in fairly long pieces of code rather than just a few short test lines, which is also why the approach in https://meta.stackoverflow.com/a/310915/ isn't always ideal
Here are some more ideas. Bob does it like this:
a = first_stage()
print(a)
# 1.234
and Charlie does it like this:
a = first_stage()
print(a) # 1.234
Now you can copy-paste the code, but clearly there are still potential drawbacks: in Bob's version, you just have to hope that the reader understands that the #
wasn't literally printed; in Charlie's, you hope they understand that the comment is console output and not a comment about the command that produced it, and you also can't handle multi-line output. And so on. Nothing I've tried is quite satisfactory under a wide range of circumstances.
My Questions
Are there good existing conventions? Or existing interface functionality or markdown tricks that help do this without too much effort? I could imagine a two-column table with code left and output right, but I believe SO markdown doesn't allow tables and can only imagine unwieldy syntax for them relative to what I'm looking for.
If not (and to save this question from being considered an entirely opinion-based comparison of possible styles): should SO implement a canonical solution for this broadly-applicable feature?