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

  • possible duplicate of [How can I do a line break (line continuation) in Python?](http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python) – ivan_pozdeev Apr 01 '14 at 13:04
  • Try your best to avoid having to use `\\` in python to have multiple line operations – Tim Apr 01 '14 at 13:04
  • Through to break a line use \ or parentheses `(` `)`, But Python Also provides `any()` and `all()` functions to write conditional expression nicely. – Grijesh Chauhan Apr 01 '14 at 15:26

2 Answers2

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):
sshashank124
  • 31,495
  • 9
  • 67
  • 76
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.

Matthew
  • 306
  • 4
  • 17