Quantcast
Channel: Active questions tagged code-formatting - Meta Stack Overflow
Viewing all articles
Browse latest Browse all 189

How to paste Python code in Stack Overflow without DIY [duplicate]

$
0
0

Here is an image of correct code (in fact, code of a previous question):

Screen image

If I copy this code and paste it in a code sample in this editor, I got this (another image):

Screen copy of Stack Overflow  result

So I'm obliged to add a tab at the beginning of the whole code (this is not valid Python code any more as can be seen by the red line under def):

Enter image description here

Then, if I paste into a code sample, I obtain the following which is not yet correct as the first line is indented twice (and we don’t have any syntax colors):

    # original code with right indentationdef animals(ani1, ani2, ani3):    if ani1 == ani2 or ani1 == ani3:        return "Match was Found"    elif ani2 == ani1 or ani2 == ani3:        return "Match was Found"    elif ani3 == ani2 or ani3 == ani1:        return "Match was Found"    else:        return "No Match Found"print(animals("dogs", "dogs", "cats"))

So I'm obliged to correct by hand to have a final result as expected:

# original code with right indentationdef animals(ani1, ani2, ani3):    if ani1 == ani2 or ani1 == ani3:        return "Match was Found"    elif ani2 == ani1 or ani2 == ani3:        return "Match was Found"    elif ani3 == ani2 or ani3 == ani1:        return "Match was Found"    else:        return "No Match Found"print(animals("dogs", "dogs", "cats"))

Then I go back to my editor (Visual Studio Code) to restore the correct indentation.

What is the best way to paste Python code directly without all this stuff?


Viewing all articles
Browse latest Browse all 189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>