-2

Would someone review these lines of code and explain me what's wrong? Why do I get the multiply statements error?

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)

The error is:

SyntaxError: multiple statements found while compiling a single statement
halfer
  • 19,824
  • 17
  • 99
  • 186
  • There is a syntax tool to post a question with correct code / indentation. Review your question and improve the syntax if you want help. – Mathieu Aug 22 '18 at 12:16
  • Your indentation doesn't make sense. Does your code actually look like this, or are you uncertain how to paste code in Stack Overflow? – John Coleman Aug 22 '18 at 12:16
  • Please fix the indentation in your question to reflect what you're actually running. – roganjosh Aug 22 '18 at 12:17
  • @Chris_Rands, yes, my mistake. I didn't realize at first that the indentation was so wrong. – John Anderson Aug 22 '18 at 12:19
  • @AbbasOFF, I have started the editing of your code for easier reading. Please continue the editing to show the actual indentation that appears in your code. – John Anderson Aug 22 '18 at 12:20
  • 2
    I would recommend 1) deleting the code, 2) repasting the code as it actually appears, 3) selecting the newly pasted code in the Stack Overflow editor, and 4) hitting `Ctrl+K` (which is the keyboard shortcut for code formatting). That way others can see what you actually see. – John Coleman Aug 22 '18 at 12:21

2 Answers2

2

As said here, you can't use multiple statements in one shell line.

Uses new line for each statement

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)

[10, -15, 3, 8, 0, 9, -6, 13, -1, 5]
[1, -1, 1, 1, 0, 1, -1, 1, -1, 1]
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

I take only assumption here. If this is your code:

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)

Well it works. You need to use multiple lines for the statements. However, you could also write your code like this:

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 0
listMask = [listMask[k] if elt != 0 else 0 for k, elt in enumerate(listOrigin)]
Mathieu
  • 5,410
  • 6
  • 28
  • 55