Hunters and Prey python


#1

Hey guys, i’m getting an error when I try to run the following code. I get “Error assigning to rvalue” please help.

def pickUpCoin():
    coin = hero.findNearest(hero.findItems())
    if coin:
        hero.move(coin.pos)
def summontroops():
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
def commandSoldier(soldier):
    enemy = soldier.findNearest(soldier.findEnemies())
    if soldier.health < 50:
        hero.command(soldier, "move", {"x": 11, "y": 50})
elif enemy:
    hero.command(soldier, "attack", enemy)
def commandArcher(soldier):
    enemy = soldier.findNearest(soldier.findEnemies())
    hero.command(soldier, "move", soldier.pos)
    if enemy:
        if soldier.distanceTo(enemy) < 25:
            hero.command(soldier, "attack", enemy)
while true:
    pickUpCoin()
    summontroops()
    friends = hero.findFriends()
    i = 0
    for i < len(friends):
        friend = friends[i]
        if friend.type == "soldier":
            commandSoldier(friend)
        elif friend.type =="archer":
            commandArcher(friend)
        i += 1

#2

Couple problems:

  • true should be True
  • In the commandSoldier function, the elif block is not indented properly.
  • for i < len(friends): should be while i < len(friends): (this is where your error comes from)
  • if hero.gold < hero.costOf("soldier"): should be >= instead of <

#3

i wish i could help you