Skip to main content
 首页 » 编程设计

python之Python 上的新行

2025年05月04日19duanxz

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): 

我正在使用 python 2.7.6 并遇到了一个我不确定的错误。这行太长了,我不得不侧滚动以查看结尾,所以我尝试添加“\n”,我认为这会让我在下一行继续代码。问题是我收到错误: “您的程序中有一个错误:行继续字符后出现意外字符”

'\n' 后没有空格或任何其他内容,所以我不确定为什么会收到此错误。

我是初学者,所以感谢任何帮助,并提前致谢

请您参考如下方法:

您只需添加 \ 而不是 \n 如下:

简单例子

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

您的案例

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):