-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
6
  • There is a syntax tool to post a question with correct code / indentation. Review your question and improve the syntax if you want help. Commented Aug 22, 2018 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? Commented Aug 22, 2018 at 12:16
  • Please fix the indentation in your question to reflect what you're actually running. Commented Aug 22, 2018 at 12:17
  • @Chris_Rands, yes, my mistake. I didn't realize at first that the indentation was so wrong. Commented Aug 22, 2018 at 12:19
  • 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. Commented Aug 22, 2018 at 12:21

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.