Cazza
Cazza

Reputation: 9

SyntaxError: unexpected character after line continuation character (backslash)

I'm trying to run the following code from a guide book and I can't tell why I have this error. Reading to errors in this categories emphasises where the problem is (the backslash) but I'm not sure how to correct this.

x_range = [dataset['RM'].min(),dataset['RM'].max()]
y_range = [dataset['target'].min(),dataset['target'].max()]
scatter_plot = dataset.plot(kind='scatter', x='RM', y='target', \xlim=x_range, ylim=y_range)
meanY = scatter_plot.plot(x_range, [dataset['target'].mean(),\  dataset['target'].mean()], '--' , color='red', linewidth=1)
meanX = scatter_plot.plot([dataset['RM'].mean(),\dataset['RM'].mean()], y_range, '--', color='red', linewidth=1)

[Code and Error]

Upvotes: 0

Views: 613

Answers (1)

Dipen Dadhaniya
Dipen Dadhaniya

Reputation: 4630

In Python, we can use \ as a line continuation character.

e.g.

# If you want it to be one long line.
long_str = '1234567890 1234567890 1234567890 1234567890 1234567890 1234567890'    

And:

# If you want to split it across multiple lines.
long_str = \
    '1234567890 1234567890 1234567890' \
    ' 1234567890 1234567890 1234567890'    

Do the same thing.


So, in your case, the code must have been something like this (using two lines instead of one long line):

scatter_plot = dataset.plot(kind='scatter', x='RM', y='target', \
    xlim=x_range, ylim=y_range)

If you want it to be in one line then you need to remove that \:

scatter_plot = dataset.plot(kind='scatter', x='RM', y='target', xlim=x_range, ylim=y_range)

Do the same for other lines.

Upvotes: 3

Related Questions