Program gave me error on a nonexistent line

During Vital Powers, I’m getting an error on line 50: “Line 50: Undefined label ‘undefined’”. The problem is, there is not line 50. Is it my code:

# This level shows how to define your own functions.
# The code inside a function is not executed immediately. It's saved for later.
# This function has your hero collect the nearest coin.
def pickUpNearestCoin():
    totalGold = 0
    items = self.findItems()
    nearestCoin = self.findNearest(items)
    if nearestCoin:
        self.move(nearestCoin.pos)
        totalGold += nearestCoin.value
        if totalGold >= 125:
            break

# This function has your hero summon a soldier.
def summonSoldier():
    # Fill in code here to summon a soldier if you have enough gold.
    if self.gold >= 20:
        self.summon("soldier")


# This function commands your soldiers to attack their nearest enemy.
def commandSoldiers():
    for soldier in self.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            self.command(soldier, "attack", enemy)
self.toggleFlowers(False)
loop:
    # In your loop, you can "call" the functions defined above.
    # The following line causes the code inside the "pickUpNearestCoin" function to be executed
    pickUpNearestCoin()
    summonSoldier()
    commandSoldiers()
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        if self.canCast("chain-lightning"):
            self.cast("chain-lightning")
        elif self.isReady("bash"):
            self.shield()
            self.bash(enemy)
        else:
            self.shield()
            self.attack(enemy)

or something else:

In your pickUpNearestCoin you have a break without any loop this is probably what is causing it.

If you want to leave a function, the correct word is return.

Also you have two loop-s. I guess you wanted to use the pickUpNearestCoin-function to break out of the first loop. That sadly is not how it works. You can use the following instead:

def pickUpNearestCoin():
    #...
    if nearestCoin:
        #...
        if totalGold >= 125:
            return True
   return False 

#...

loop:
    shouldBreak = pickUpNearestCoin()
    if shouldBreak:
        break
    #...

loop:
    #...
1 Like