1
while (0 > ship_row_1_1 or ship_row_1_1 > 9) or (0 > ship_row_1_2 or ship_row_1_2 > 9)\n
or(0 > ship_col_1_1 or ship_col_1_1 > 9) or (0 > ship_col_1_2 or ship_col_1_2 > 9):

I'm using python 2.7.6 and ran into an error I'm unsure of. This line was to long and I was having to side scroll to view the end so I tried adding the '\n' which I thought would let me continue the code on the next line. The problem is I get the error: "There's an error in your program: unexpected character after line continuation character"

There's no space or anything after the '\n' so I'm unsure why I'm getting this error.

I'm a beginner so any help is appreciated and thanks in advance

3
  • possible duplicate of How can I do a line break (line continuation) in Python? Commented Apr 1, 2014 at 13:04
  • Try your best to avoid having to use `\` in python to have multiple line operations Commented Apr 1, 2014 at 13:04
  • Through to break a line use \ or parentheses ( ), But Python Also provides any() and all() functions to write conditional expression nicely. Commented Apr 1, 2014 at 15:26

2 Answers 2

5

You simply add a \ and not a \n as follows:

Simple example

def say_hi():
    while True\
        and 1==1\
        and 2==2:
        print 'hello'

Your Case

while (0 > ship_row_1_1 or ship_row_1_1 > 9) or (0 > ship_row_1_2 or ship_row_1_2 > 9)\
or(0 > ship_col_1_1 or ship_col_1_1 > 9) or (0 > ship_col_1_2 or ship_col_1_2 > 9):
Sign up to request clarification or add additional context in comments.

4 Comments

@user3485148, If my answer was helpful, would you mind accepting it? Thanks
Sshashank single # heading looks too large, try with three ### I think you will better like.
@Grijesh, Ah, I didn't even know that. Thank you very much
yes I knew you are new at SO. I never read markdown notes Whenver I see a nicely formatted answer (that I can't do) I just ckick on edit to see how answer is written. :)
1

'\n' is the newline character. It only has meaning inside strings, where it splits the line. If you want to continue a line of code, just use a single '\'. The error is saying that Python doesn't know what to do with the 'n' it found after you told it with the '\' that you were going to continue on the next line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.