Invalid Syntax Error for an If Statement in a Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clabbage
    New Member
    • Oct 2013
    • 3

    Invalid Syntax Error for an If Statement in a Function

    For some odd reason my code was all working fine until I made a few changes which broke almost everything in the function. I've since removed said changes but it's still broken. It's saying the colon in this section is Invalid Syntax. In fact it's saying the same for every if statement as well as some of the comparison operators.

    Code:
    if difficulty == "e":
      number_enemies -= 1
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    Hi, can you post all your code?

    Comment

    • Clabbage
      New Member
      • Oct 2013
      • 3

      #3
      The whole function is quite long, but sure. It's supposed to create the enemies in a text game I'm working on.

      Code:
      # Randomly generates enemies
      def create_enemies(difficulty, mode, current_location, battles):
      
        # Clears enemies from the last battle
        enemies = []
      
        # List of possible names for the enemies to use
        names = ["Aiden", "James", "Sam", "Aidan", "Brad", "Pavle", "Fuda", "David", "Andrew", "Harry", "Callum", "Tomasz", "Bazli", "Georgia", "Sheldon", "Legolas", "Aragorn", "Gimli", "Boromir", "Faramir", "Éomer", "Éowyn", "Treebeard", "Celeborn", "Galadrial", "Radagast", "Gandalf", "Merry", "Pippin", "Frodo", "Sam", "Bilbo", "Azog", "Gorkil", "Sharku", "Shagrat", "Gorbag", "Lurtz", "Tom Bombadil", "Goldberry"]
      
        # Randomly decides how many enemies there will be
        number_enemies = int(random.randint(2,5)
        
        # 1 less enemy in Easy difficulty
        if difficulty == "e": 
          number_enemies -= 1
        # 1 more enemy in Hard difficulty
        elif difficulty == "h":
          number_enemies += 1
        # 2 more enemies in Legendary difficulty
        elif difficulty == "l":
          number_enemies += 2
      
        # The more battles you've had, the more enemies there will be
        number_enemies += int((battles/100)*30)
          
        # There will always be at least 2 enemies
        if not (number_enemies >= 2):
          number_enemies = 2
      
        #Enemies are different at the boss battles
        if mode == 2 and current_location.evil_final == True:
          enemies = [Troll("Cave Troll"), Uruk("Uruk"), Wizard("Sauron"), Orc("Orc"), WargRider("Warg Rider"), Easterling("Easterling"), Goblin("Goblin 1"), Goblin("Goblin 2"), Goblin("Goblin 3")]
        elif mode == 1 and current_location.good_final == True:
          enemies = [Wizard("Gandalf"), Elf("Legolas"), Dwarf("Gimli"), Ranger("Aragorn"), Human("Boromir"), Hobbit("Frodo"), Hobbit("Sam"), Hobbit("Merry"), Hobbit("Pippin")]
        else:
          for z in range(number_enemies):
            if mode == 1:
              # Determines the class of the enemy
              # 1/4 Goblin, 1/4 Orc, 1/8 Uruk, 1/8 Easterling, 3/24 Warg Rider, 2/24, Wizard, 1/24 Troll
              enemy_class = random.randint(1, 24)
              if enemy_class in range(1,6):
                enemies.append(Goblin(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(7,12):
                enemies.append(Orc(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(13,15):
                enemies.append(Uruk(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(16,18):
                enemies.append(Easterling(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(19,21):
                enemies.append(WargRider(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(22,23):
                enemies.append(Wizard(names[random.randint(1,(len(names)-1))]))
              if enemy_class == 24:
                enemies.append(Troll(names[random.randint(1,(len(names)-1))]))
      
            if mode == 2:
              # Determines the class of the enemy
              # 1/4 Hobbit, 1/4 Human, 1/8 Elf, 1/8 Dwarf, 3/24 Ranger, 2/24 Wizard, 1/24 Ent
              enemy_class = random.randint(1, 24)
              if enemy_class in range(1,6):
                enemies.append(Hobbit(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(7,12):
                enemies.append(Human(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(13,15):
                enemies.append(Elf(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(16,18):
                enemies.append(Dwarf(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(19,21):
                enemies.append(Ranger(names[random.randint(1,(len(names)-1))]))
              if enemy_class in range(22,23):
                enemies.append(Wizard(names[random.randint(1,(len(names)-1))]))
              if enemy_class == 24:
                enemies.append(Ent(names[random.randint(1,(len(names)-1))]))
        
        return enemies

      Comment

      • stdq
        New Member
        • Apr 2013
        • 94

        #4
        Thanks! I managed to run the code after a few changes. I saved it in txt because I couldn't upload it here in .py format. I began selecting all text and then pressing shift + tab twice; then, there was also a missing parentheses at the end of 11. This file, in .py format, ran ok. Sometimes, the error can be in a line other than the one indicated. Please let me know if you have any doubts. By the way, I changed the file encoding to UTF-8 due to an error that I got in here, but it may have been unnecessary. Please let me know if you still have any problems.

        Maybe the only change you need to make is to add the missing parenthesis to your code.
        Attached Files

        Comment

        • Clabbage
          New Member
          • Oct 2013
          • 3

          #5
          That missing parentheses... I put it there and suddenly everything works perfectly. Thank you so much!

          Comment

          • stdq
            New Member
            • Apr 2013
            • 94

            #6
            You're welcome!

            Best,
            stdq

            Comment

            Working...