I have an issue with trying to under the syntax error with the line if enemy_select = 1: in this type of code:

enemy_select = random.randit(1, 11)

for enemy_select in range(1):
    if enemy_select = 1:
        enemy_type = character 1

    elif enemy_select = 2:
        enemy_type = character 2

    elif enemy_select = 3:
        enemy_type = character 3

    elif enemy_select = 4:
        enemy_type = character 4

    elif enemy_select = 5:
        enemy_type = character 5

    elif enemy_select = 6:
        enemy_type = character 6

    elif enemy_select = 7:
        enemy_type = character 7

    elif enemy_select = 8:
        enemy_type = character 8

    elif enemy_select = 10:
        enemy_type = character 9

    elif enemy_select = 10:
        enemy_type = character 10

    elif enemy_select = 11:
        enemy_type = character 11
  • \$\begingroup\$ What exactly does the syntax error say? \$\endgroup\$ – Philipp Jul 26 '18 at 13:39
  • \$\begingroup\$ It just returns it as an invalid syntax \$\endgroup\$ – kayra yorulmaz Jul 26 '18 at 13:41
  • \$\begingroup\$ In the future, please try to reduce the problem before posting code - if all the trail elifs aren't part of the problem (i.e. problem remains unchanged after removing them), don't include them. Short, easy to read code is more likely to get get answers & often you resolve the issue on your own when narrowing it down. \$\endgroup\$ – Pikalek Jul 26 '18 at 15:13

It seems you lack the correct indentation for all your lines after for....

Add few spaces or tab at the beginning of every line, starting from first if.


I was wrong, the problem is that you're using = instead of == in if lines.

BTW: I've tried it in CMD window and it has shown me a ^ sign at the exact character that caused an error. May be helpful in the future.

  • \$\begingroup\$ Even after tabbing every line (Starting from 'if') it still says that there is a syntax error. Not to mention that wouldn't it give an indentation error if the indentation was incorrect? \$\endgroup\$ – kayra yorulmaz Jul 26 '18 at 14:18
  • \$\begingroup\$ I've updated my answer. \$\endgroup\$ – kolenda Jul 26 '18 at 14:43
  • \$\begingroup\$ I'm not that familiar with Python, even though = instead of == is an error, wouldn't that be a logic error? As in the compiler can't catch it no matter what? At least that's the case in C. \$\endgroup\$ – TomTsagk Jul 26 '18 at 15:16
  • \$\begingroup\$ In C you can write if( i=1 ) and it compiles but C allows you to do a lot of such things. Python seems to be more restrictive and assignment operation is not the one he expects in an if statement, so it throws a generic "syntax error". I still agree that the message could be better. \$\endgroup\$ – kolenda Jul 27 '18 at 8:51
  • \$\begingroup\$ Tried to fix it, just gave some generic "indentation error" or just replies a syntax error at the end of the code \$\endgroup\$ – kayra yorulmaz Jul 27 '18 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.