-1

I thought Python supported single line if statements but I am getting an error with += on both Leetcode and Repl. The double line one works so I am here mostly to figure out the inner workings of Python.

Based on this question I thought this would work. I would like to know if this is a Python or platform (Leetcode or Replit) issue.

Here's my replit code which I have pasted below for posterity.

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        count = 0
        
        for num in nums:
            count += 1 if len(str(num)) % 2 == 0
            
        return count

nums = [12,345,2,6,7896]
s = Solution()
print(s.findNumbers(nums))

My error is:

File "main.py", line 8
    count += 1 if len(str(num)) % 2 == 0
                                       ^
SyntaxError: invalid syntax
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
  • 1
    You may be thinking of Perl or Ruby, both of which do actually support postfix conditionals in the exact form you just described. – Silvio Mayolo Aug 06 '21 at 23:23
  • 1
    Just like what you can see in the referred topic, you need the `else` here too. So it's `count += 1 if len(str(num)) % 2 == 0 else 0` what you can do. `+=` needs an increment for both cases. – tevemadar Aug 06 '21 at 23:24
  • Why is this closed? This assumes that someone should know the ternary operator which is exactly what this question is asking. – heretoinfinity Aug 07 '21 at 01:00
  • I didn't vote for closure, but I assume it's partially about the fact that the `+=` operator has nothing to do with what this question is really about. – Captain Trojan Aug 07 '21 at 01:02

3 Answers3

6

Sure! Just turn that into this:

count += 1 if len(str(num)) % 2 == 0 else 0

You need to finish the whole statement.

The 1 if len(str(num)) % 2 == 0 needs to have a determined value. Adding 0 will suffice for you, but beware, this is not always possible. Keep in mind that this is not an if statement, but a ternary operator, which are two different things.

Captain Trojan
  • 2,800
  • 1
  • 11
  • 28
  • In what cases would it not be possible? – heretoinfinity Aug 06 '21 at 23:53
  • If for example you were adding a result of a function `f`, whose argument would be your ternary operator, to `count`, where `f(x)` was never zero. The issue with this approach is that you always *must* add something to `count`, even if you don't want to. If you really wanted to use the ternary operator to inline a simple `if` statement, that's a very poor usage imho. You can inline the statement into `if cond: count += 1` if you wanted to. – Captain Trojan Aug 07 '21 at 01:00
1

In that particular context, an if needs an else clause. So, you could do:

count += 1 if len(str(num)) % 2 == 0 else 0

Or, if you want to be Pythonicly fancy, where booleans have integer values:

count += len(str(num)) % 2 == 0

There is a form of a single-line if that doesn't require an else clause, which is used in list expressions, like this:

nums = [n for n in range(100) if n % 2 == 0]

(yes, yes, I know you could just use a step with range, it's an example!)

sj95126
  • 6,520
  • 2
  • 15
  • 34
1

Regular assignment doesn't work either:

>>> count = 1 if len(str(num)) % 2 == 0
  File "<stdin>", line 1
    count = 1 if len(str(num)) % 2 == 0
                                      ^
SyntaxError: invalid syntax

The problem is that everything on the right side of the equals needs to fully resolve to a single object before any assignment on the left side can happen. What would this object be if no object were resolved? You want conditional assignment, but both = and += are unconditional assignment.

You could do

count += 1 if len(str(num)) % 2 == 0 else 0

but you'd be laughed out of the code review. Just let if be if:

if len(str(num)) % 2 == 0:
    count += 1 
tdelaney
  • 73,364
  • 6
  • 83
  • 116