# I rly need help (borrowed sword)

**URL:** https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613
**Category:** Level Help
**Created:** [July 12, 2021, 7:38am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613 "2021-07-12T07:38:08Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 7:38am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/1 "2021-07-12T07:38:08Z")

</div>

ive been stuck for days here, i dont know what i am doing wrong ive tried like 30 totally different pieces of code. this is what ive ended up with now, the def(): pices have defined everything yet it says ogre isnt defined and then the yeti and then b (and c) (i do some pices of code saying yeti = 0 and ogre = 0 and c = hero.findNearestEnemy so that it is defined at the start and the code can fix it later and it just stops when the first guy is defeated

```python
def findHealth():
    for enemy in hero.findEnemies():
        ogre = 0
        yeti = 0
        if enemy.type == "yeti":
            yeti += enemy.health
        else:
            ogre += enemy.health

def nearYetiAttack():
    for b in hero.findEnemies():
        if b.type == "yeti":
            bad = b

def nearOgreAttack():
    for c in hero.findEnemies():
        if c.type == "ogre" or "scout":
            reallyBad = c

def findBest():
    if ogre >= yeti:
        nearYetiAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", b)
    else:
        nearOgreAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", c)

while True:
    findHealth()
    findBest()

```

help

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 7:48am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/2 "2021-07-12T07:48:20Z")

</div>

i did this and they just stop like it wont define anything else

```python
c = hero.findNearestEnemy()
b = hero.findNearestEnemy()
yeti = 0
ogre = 0

def findHealth():
    for enemy in hero.findEnemies():
        ogre = 0
        yeti = 0
        if enemy.type == "yeti":
            yeti += enemy.health
        else:
            ogre += enemy.health

def nearYetiAttack():
    for b in hero.findEnemies():
        if b.type == "yeti":
            bad = b

def nearOgreAttack():
    for c in hero.findEnemies():
        if c.type == "ogre" or "scout":
            reallyBad = c

def findBest():
    if ogre >= yeti:
        nearYetiAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", b)
    else:
        nearOgreAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", c)

while True:
    findHealth()
    findBest()

```

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 8:05am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/3 "2021-07-12T08:05:55Z")

</div>

i now have everything running through each other so in order to get to one part you need to define another and it still says “ogre is not defined”

```python
def findHealth():
    for enemy in hero.findEnemies():
        ogre = 0
        yeti = 0
        if enemy.type == "yeti":
            yeti += enemy.health
        else:
            ogre += enemy.health

def nearYetiAttack():
    bad = hero.findByType("yeti")
    if bad:
        b = hero.findNearest(bad)
        for friend in hero.findFriends:
            hero.command(friend, "attack", b)

def nearOgreAttack():
    reallyBad = hero.findByType("brawler")
    if reallyBad:
        c = hero.findNearest(reallyBad)
        for friend in hero.findFriends:
            hero.command(friend, "attack", c)

def findBest():
    findHealth()
    if ogre >= yeti:
        nearYetiAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", b)
    else:
        nearOgreAttack()
        for friend in hero.findFriends():
            hero.command(friend, "attack", c)

while True:
    findBest()

```

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 8:10am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/4 "2021-07-12T08:10:30Z")

</div>

ive given up im now just killing the yetis because theyre winning

```python
while True:
    yeti = hero.findNearest(hero.findByType("yeti"))
    if yeti:
        for friend in hero.findFriends():
            hero.command(friend, "attack", yeti)

```

---

<div class="post-metadata">

### Author: ![Red-backspider](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/red-backspider/32/23212_2.png) [@Red-backspider](https://discourse.codecombat.com/u/Red-backspider)
#### Post date: [July 12, 2021, 11:00am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/5 "2021-07-12T11:00:59Z")

</div>

What level are you stuck on?

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 5:27pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/6 "2021-07-12T17:27:24Z")

</div>

Oh i thought i mentioned borrowed sword  
sorry

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 5:28pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/7 "2021-07-12T17:28:20Z")

</div>

wait i am going to go do something

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 5:36pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/8 "2021-07-12T17:36:02Z")

</div>

i realized some of my code was repeating itself and trying to attack twice so i fixed that i was tired lol

```python
def nearYetiAttack():
    bad = hero.findNearest(hero.findByType("yeti"))
    if bad:
        for friend in hero.findFriends:
            hero.command(friend, "attack", bad)

def nearOgreAttack():
    reallyBad = hero.findNearest(hero.findByType("brawler"))
    if reallyBad:
        for friend in hero.findFriends:
            hero.command(friend, "attack", reallyBad)

def findBest():
    for enemy in hero.findEnemies():
        ogre = 0
        yeti = 0
        if enemy.type == "yeti":
            yeti += enemy.health
        else:
            ogre += enemy.health
    if ogre >= yeti:
        nearOgreAttack()
    else:
        nearYetiAttack()

while True:
    findBest()

```

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 5:37pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/9 "2021-07-12T17:37:40Z")

</div>

Now there are no errors but it still kills brawlers even thought the yetis clearly have more health

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 12, 2021, 6:01pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/10 "2021-07-12T18:01:32Z")

</div>

and i also have a problem with another level.

```python
def ifBrawler():
    bad = hero.findNearest(hero.findByType("Brawler" or "Headhunter" or "Ogre"))
    if bad and hero.distanceTo(bad) < 8:
        hero.consecrate()
        hero.attack(bad)
    elif bad:
        hero.attack(bad)

```

```python
while True:
    notOk = hero.findNearest(hero.findByType("Headhunter" or "Ogre" or "Brawler"))
    if notOk:
        ifBrawler()
    else:
        pickUpNearestCoin()
        summonSoldier()
        commandSoldiers()

```

it goes straight to pickUpNearestCoin() even if there are brawlers or ogres or headhunters. Its like it wont look, and yes, i started off not capitalizing the ogre or headhunter etc (you can put multiple things in findbytype right?

edit: creating another topic for this

---

<div class="post-metadata">

### Author: ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)
#### Post date: [July 13, 2021, 2:23am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/11 "2021-07-13T02:23:22Z")

</div>

link? i cant seem to find the level

---

<div class="post-metadata">

### Author: ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)
#### Post date: [July 13, 2021, 2:24am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/12 "2021-07-13T02:24:07Z")

</div>

nvm i found the level looking into it 👍

---

<div class="post-metadata">

### Author: ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)
#### Post date: [July 13, 2021, 2:31am UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/13 "2021-07-13T02:31:20Z")

</div>

looks like you should add bad.health \>= reallyBad.health then on lines 3(just after the if bad part)  
and vice versa for line 9(so reallyBad.health \>= bad.health and so on)

---

<div class="post-metadata">

### Author: ![R\_Ross](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/r_ross/32/22031_2.png) [@R\_Ross](https://discourse.codecombat.com/u/R_Ross)
#### Post date: [July 13, 2021, 7:29pm UTC](https://discourse.codecombat.com/t/i-rly-need-help-borrowed-sword/29613/14 "2021-07-13T19:29:35Z")

</div>

thank you ill try it
