-8

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 indentation
def 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 indentation
def 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?

6
  • 10
    Paste the code, select it, then press the 'Code sample' button {} or Ctrl-K ? Commented Aug 12, 2022 at 16:07
  • my question will be soon blocked by administrators as not useful etc. I take time to redact precisely the case and i'm not probably the unique guy that see 'code icon' in the editor and think this must work seeing 'paste code here' . Thanks to those who responded.
    – pirela
    Commented Aug 12, 2022 at 16:14
  • 5
    I don't really get what your problem is. Did you press the code sample button before pasting your code? There is no reference in your question to pressing this button. Commented Aug 12, 2022 at 16:18
  • 1
    yes, i press the code sample and see 'paste your code here', what i do with explained defects. By the way, thank you for not tossing my question like an old rag :)
    – pirela
    Commented Aug 12, 2022 at 16:21
  • 2
    Well, I had never tried to press it before, but I find the behaviour in this case misleading, and problematic regarding indentation. Maybe it would be better if it did nothing in this case... Commented Aug 12, 2022 at 16:39
  • Pressing the button first is designed for the case where you are going to immediately thereafter type in a single line of code. It's not meant for when you are going to copy-paste in a block of code. For what it's worth, we did recently have a discussion about this: meta.stackoverflow.com/questions/419468/… @ThierryLathuille
    – Cody Gray Mod
    Commented Aug 12, 2022 at 22:23

1 Answer 1

17

You can always use three backticks (```) and prevent the need to indent the code:

```
code
```

Or select and use Ctrl + K to indent automatically.

1
  • What kind of grinds my gears is that this is explained in the documentation... but you have to click a very small print "show more" link to see it :/ I'm sorry, but that looks like someone is trying to make it hard to find relevant information. As Reddit would put it, a-hole design.
    – Gimby
    Commented Aug 18, 2022 at 11:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.