0

I was reading that Python allows for implicit line continuation as long as you are in brackets or parentheses; however in VS Code it doesn't seem to allow this behavior for me, and I'm required to use the explicit forward slash line continuation.

plt.title("Monte Carlo Dice Game [" + str(num_simulations) + "
      simulations]")

Pylance error triggered: VS Code Error

Is there some sort of setting which I need to enable to allow implicit line continuation?

Tried using a return/linebreak within the parentheses of a function call, expecting no errors to pop up within VS Code, but an error occurred.

  • 1
    VS code can only warn, it can't "forbid" anything, it's not running your code. The error you're showing comes from your PyLance plugin (because VS code itself has no idea what Python is), so just configure it to ignore that specific rule. – Mike 'Pomax' Kamermans Jun 30 '23 at 22:57
  • @Mike This is a syntax error, it can't be ignored. I'll post more details in a sec. – wjandrea Jun 30 '23 at 22:58
  • 1
    Becuase the string is not terminated. Don't paste code as images. – Diego Torres Milano Jun 30 '23 at 22:58
  • [Please post the code itself](https://meta.stackoverflow.com/q/285551/4518341). The screenshot is fine for illustration though. You can [edit] your post and use [code formatting](/editing-help#code). BTW, welcome back to Stack Overflow! Check out the [tour], and [ask] if you want tips for future questions. – wjandrea Jun 30 '23 at 23:01
  • I thought there was an existing question about this, but I only found vaguely related ones. Anyway, the others here have said what I wanted. – wjandrea Jun 30 '23 at 23:12
  • @Barmar Technically, the line break here can be in the middle of an *expression* (addition), and a line break can't occur in any literal (string literal here). – wjandrea Jun 30 '23 at 23:20

2 Answers2

1

If you move the quote to the next line it will work

... str(whatever) +
    "simulations]")

you can also do like

... str(whatever)
    + "simulations]")
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Per Barmar:

You can have line breaks in argument lists, but you can't have them inside strings unless you use triple quotes. – Barmar 3 hours ago

Not sure where this is documented officially but it seems to be the best answer.

  • The relevant docs on lexical analysis of Python scripts is found here https://docs.python.org/3/reference/lexical_analysis.html#implicit-line-joining – nigh_anxiety Jul 01 '23 at 05:26
  • Thank you nigh! It seems there is some roundabout discussion about how newlines are treated in the "notes" area of section 2.4 discussing string literals as well: "A backslash can be added at the end of a line to ignore the newline". So it seems within a string literal a newline is a component of the literal, and that's why implicit line continuation doesn't work within the string literal itself. – hypnoticbanana Jul 01 '23 at 20:03