++ operator i'm in python

I am trying to translate one of my Java projects into Python, and I am having problems with one specific line. Java Code:

if (++j == 9)
    return true;

I think it should be in python

if (j += 1) ==9:
        return True

... but I get an error SyntaxError: invalid syntax.

How can I translate this Java to Python?

+5
source share
6 answers

Yes, this is really a syntax error.

You probably want:

j += 1
if j == 9:
  return True

The reason is that python requires an expression after the keyword if( docs ), whereas it j += 1is an operator.


And congratulations, you just dodged a bullet without translating it into:

if (++j == 9):
    return True

which is valid Python code and will almost certainly be a mistake!

+13
source

, Python ++.

j += 1
if j == 9:
  return True

wim, if -, True False. Java ++j . j, .

+1

++ Python. :

j += 1
if j == 9:
    return True
0

+ = . . :

j+=1
if j==9:
   return True
0
  • Python ++ ( --).
  • j += 1 , .

, :

if j == 8:
    return True

j - global ( ), :

j += 1
if j == 9:
    return True
0

j += 1 j = j + 1. , j. , , j + 1 == 9, :

if (j += 1) ==9:
    return True

j += 1
if j == 9:
    return True
0

All Articles