猿问

如何在Python中调试“多个语句”错误?

有人会审查这些代码行并向我解释什么地方出问题吗?为什么会出现乘法语句错误?


listOrigin = [10, -15, 3, 8, 0, 9, -6, 13, -1, 5]

listMask = []

for item in listOrigin:

    if item > 0:

        listMask.append(1)

    elif item < 0:

        listMask.append(-1)

    else:

        listMask.append(0)


print(listOrigin)

print(listMask)

错误是:


SyntaxError: multiple statements found while compiling a single statement


qq_遁去的一_1
浏览 214回答 2
2回答

皈依舞

至于说在这里,你不能在一个外壳线使用多个语句。为每个语句使用新行listOrigin = [10, -15, 3, 8, 0, 9, -6, 13, -1, 5]listMask = []for item in listOrigin:&nbsp; &nbsp; &nbsp;if item > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listMask.append(1)&nbsp; &nbsp; &nbsp;elif item < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listMask.append(-1)&nbsp; &nbsp; &nbsp;else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listMask.append(0)print(listOrigin)print(listMask)[10, -15, 3, 8, 0, 9, -6, 13, -1, 5][1, -1, 1, 1, 0, 1, -1, 1, -1, 1]

慕仙森

我只在这里假设。如果这是您的代码:listOrigin = [10, -15, 3, 8, 0, 9, -6, 13, -1, 5]listMask = []for item in listOrigin:&nbsp; &nbsp; if item > 0:&nbsp; &nbsp; &nbsp; &nbsp; listMask.append(1)&nbsp; &nbsp; elif item < 0:&nbsp; &nbsp; &nbsp; &nbsp; listMask.append(-1)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; listMask.append(0)print(listOrigin)print(listMask)那么它的工作原理。您需要对语句使用多行。但是,您也可以这样编写代码:listOrigin = [10, -15, 3, 8, 0, 9, -6, 13, -1, 5]# Place a 1 if the item is above 0, else a -1. 0 will be flagged as -1.listMask = [1 if elt > 0 else -1 for elt in listOrigin]# Place the 0listMask = [listMask[k] if elt != 0 else 0 for k, elt in enumerate(listOrigin)]
随时随地看视频慕课网APP

相关分类

Python
我要回答