python - Python 上的新行

标签 python

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

关于python - Python 上的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22786677/

相关文章:

python - 数字列表的校验和

python - 在 Python 中将 IPv4 地址转换为十六进制 IPv6 地址

python - 使用行列表,无论我有 1 行还是 N 行

python - setdefault 与 defaultdict 性能

python - 为 scrapy 安装 python - 符号链接(symbolic link)和权限问题

python - Pearson 的卡方测试 Python

python - Funcparserlib.lexer.Spec 导入错误 : cannot import name 'Spec'

python cPickle unpickling错误无效加载 key

python - Pandas Series 写入和读取 json 数据时会出现带有 to_json 和 read_json 的 ValueError

python - 多处理池和 Lightgbm