# \[SOLVED\] Village Champion issues

**URL:** https://discourse.codecombat.com/t/solved-village-champion-issues/13952
**Category:** Uncategorized
**Created:** [February 21, 2018, 4:45pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952 "2018-02-21T16:45:41Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![jowe11](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jowe11/32/7525_2.png) [@jowe11](https://discourse.codecombat.com/u/jowe11)
#### Post date: [February 21, 2018, 4:45pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/1 "2018-02-21T16:45:41Z")

</div>

![15](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/03f6d150bfd53863580c200df758811955ad2205.jpg)

I’m not sure what I’m doing wrong. I don’t understand where the error is at. I thought that I defined a function cleaveOrAttack there at the beginning of my code. Perhaps I don’t understand the terminology? Please help

```python
# Incoming munchkins! Defend the town!

# Define your own function to fight the enemy!
cleaveOrAttack = (enemy) =>
    enemy = @findNearestEnemy()
    if enemy
      if @isReady 'cleave'
        @cleave enemy
      else
        @attack enemy
        
# Move between patrol points and call the function.
while true
  @moveXY 35, 34
    # Use cleaveOrAttack function you defined above.
  cleaveOrAttack()

  @moveXY 47, 27
    # Call the function again.
  cleaveOrAttack()
 
  @moveXY 60, 31
    # Call the function again.
  cleaveOrAttack()
    

```

---

<div class="post-metadata">

### Author: ![jowe11](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jowe11/32/7525_2.png) [@jowe11](https://discourse.codecombat.com/u/jowe11)
#### Post date: [February 21, 2018, 5:43pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/2 "2018-02-21T17:43:39Z")

</div>

I figured it out. I was passing info into the function that I wrote (enemy). After removing enemy out of it, it works and passes.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 11, 2018, 12:49am UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/3 "2018-04-11T00:49:15Z")

</div>

# Incoming munchkins! Defend the town!

# Define your own function to fight the enemy!

def cleaveOrAttack():  
# In the function, find an enemy, then cleave or attack it.  
target = hero.findNearestEnemy()  
if enemy:  
if hero.isReady(“cleave”):  
hero.cleave(target)  
else:  
hero.attack(enemy)

# Move between patrol points and call the function.

while True:  
hero.moveXY(35, 34)  
# Use cleaveOrAttack function you defined above.  
target = hero.findNearestEnemy()  
if target:  
hero.cleaveOrAttack(target)  
hero.moveXY(47, 27)  
# Call the function again.  
target= hero.findNearestEnemy()  
if target:  
hero.cleaveOrAttack(target)  
hero.moveXY(60, 31)  
# Call the function again.  
target= hero.findNearestEnemy()  
if target:  
hero.cleaveOrAttack(enemy)

That’s the code I have used and its not working, Ive probably missed something really basic or Ive just done it completely wrong…any advice would be appreciated.

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 11, 2018, 1:41am UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/4 "2018-04-11T01:41:36Z")

</div>

Please learn to post your code properly. It just requires a small amount of effort.

 ![FormatCode](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/03e091050fe4eed8c0076eee4ab979e171ba14c3.jpg)

To post your code **_from the game_** , use the **\</\>** button or it won’t format properly. When you click the **\</\>** button, the following will appear:

 ![PostCode](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/7/78430fb6ee8dec450c5a0355db89d0dc189030b4.JPG)

Please paste **ALL** of your code inside the triple back tick marks.

# ``` \<— Triple back tick marks.

**Paste ALL of your code in here.**

# ``` \<— Triple back tick marks.

There are many people here willing and able to help. If you use the **\</\>** button correctly, then **ALL** of your code should look like this:

```python
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

```

If **ALL** of your code isn’t formatted like the code above, then you’re doing it wrong and **_we can’t see the structure of the code_** to troubleshoot whether or not that is the issue. Use the **\</\>** button and **_help us help you_**. This works when sending a private message as well.

Thank you.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 11, 2018, 1:18pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/5 "2018-04-11T13:18:41Z")

</div>

```python
    # In the function, find an enemy, then cleave or attack it.
    target = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(target)
    else:
        hero.attack(enemy)

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    target = hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(target)
    hero.moveXY(47, 27)
    # Call the function again.
    target= hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(target)
    hero.moveXY(60, 31)
    # Call the function again.
    target= hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(enemy) 

type or paste code here

```

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 11, 2018, 1:19pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/6 "2018-04-11T13:19:13Z")

</div>

I hope thats right now 🙂

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 11, 2018, 1:52pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/7 "2018-04-11T13:52:59Z")

</div>

Much better.

Half your code uses the variable “target” and the other half uses an undefined variable “enemy”. Pick one and stick with it.

Also, when you call your function, you don’t have to put hero. in front of it.

You already have an if enemy conditional in the function, so you don’t have to use one in your while True loop. Just move to the xy coordinates and call the function.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 11, 2018, 2:05pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/8 "2018-04-11T14:05:30Z")

</div>

Ill try that however it says in the guide that Cleave changes enemy to target, is that correct???so which is the correct to use i mean will it make a difference …and that is why I have done half that way where cleave is used I have used Target…Thank you.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 3:02pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/9 "2018-04-12T15:02:30Z")

</div>

No I think my code is a complete mess…somewhere along the lines I have overthought it and confused myself…Ive tried constantly over the last few days with a headache to fix it I may just scrap it all and start from fresh. What I do find misleading in game I could be wrong as Ive been struggling to think straight over the last few days but an arrow sends you to village champion first which i struggled with for hours and when I looked in advice it mentions doing Village Warder first. Probably my brain it confused me anyhow skipping to a more complicated part without understand the previous if that makes sense.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 3:05pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/10 "2018-04-12T15:05:09Z")

</div>

```python
# Incoming munchkins! Defend the town!

# Define your own function to fight the enemy!

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else:
        if enemy:
            hero.attack(enemy)
    pass
# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    enemy= hero.findNearestEnemy()
    if enemy:
            cleaveOrAttack(enemy)
            
    hero.moveXY(47, 27)
    # Call the function again.
    enemy = hero.findNearestEnemy()
    if enemy:
            cleaveOrAttack(enemy) 

    hero.moveXY(60, 31)
    # Call the function again.
    if enemy:
        cleaveOrAttack(enemy)
type or paste code here

```

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 3:06pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/11 "2018-04-12T15:06:00Z")

</div>

I put this and its still doesn’t work.  
back to scratch I guess then.

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 12, 2018, 3:09pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/12 "2018-04-12T15:09:24Z")

</div>

In your function, the else statement doesn’t need an if enemy conditional statement after it. Read my comments in your code below.

```python
def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else: # this conditional statement should line up with the if hero.isReady("cleave")
        if enemy: # eliminate this line
            hero.attack(enemy)
    pass

```

Think of it like this,

```python
if enemy:
    if hero is ready:
        do this
    else: # if hero is NOT ready
        do that

```

In your while True loop, just moveXY and call the function. You don’t need to redefine the enemy variable or check for an enemy again. This is all taken care of in your function.

```python
hero.moveXY(x, y)
cleaveOrAttack()

```

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 3:41pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/13 "2018-04-12T15:41:46Z")

</div>

```python
Ok so tried this as you mention. However I still get an Error msg saying "There is no enemy. Use Enemy = hero.findNearestEnemy()" which was why I started redefining the enemy variable int he first place.It does look like I was overcomplicating things but even simplified I get the error msg.

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else:
        hero.attack(enemy)
    pass
# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack(enemy)
            
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack(enemy) 
            
    hero.moveXY(60,31)
    # Call the function again.
    cleaveOrAttack(enemy) 

type or paste code here

```

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 3:47pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/14 "2018-04-12T15:47:32Z")

</div>

My sincerest apologies if I am being very stupid here…it looks like this should be really simple as you have shown but its not working…Ive got a nasty headache and 2 year old constantly screaming for attention …try my best a the moment…the closest I got was Keeping the redefined variables in but even then 1 peasant keeps dying so I must be doing something wrong.

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 12, 2018, 3:48pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/15 "2018-04-12T15:48:46Z")

</div>

The structure of your function is still incorrect. Review the above corrections. Also, there should not be arguments in the parenthesis when you call your function. You should call the function just like it appears in the definition.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 4:46pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/16 "2018-04-12T16:46:42Z")

</div>

[quote=“MunkeyShynes, post:12, topic:13952”]  
Ok so going back to your comments and checking I’ve done them.

1)Half your code uses the variable “target” and the other half uses an undefined variable “enemy”. Pick one and stick with it. Done!

1. “Also, when you call your function, you don’t have to put hero”. in front of it.Done removed!

3)“You already have an if enemy conditional in the function, so you don’t have to use one in your while True loop. Just move to the xy coordinates and call the function”. Ok removed!

1. “In your function the else statement doesn’t need an if enemy conditional statement after it”. Removed Done.!

5)“Read my comments in your code below.” Done!

6)“In your while true loop just move XY.” (I don’t think I have the coordinates wrong do I?)

1. “And call the function. You don’t need to redefine the enemy variable or check for an enemy again.” This is all taken care of in your function. Done I believe!

[quote=“MunkeyShynes, post:15, topic:13952”]  
Also, there should not be arguments in the parenthesis when you call your function. You should call the function just like it appears in the definition.  
[/quote] Done too I think

so this is what I have now and I get this error message`def cleaveOrAttack():  
# In the function, find an enemy, then cleave or attack it.  
def cleaveOr Attack():  
enemy = hero.findNearestEnemy()  
if enemy:  
if hero.isReady(“cleave”):  
hero.cleave(enemy)  
else:  
hero.attack(enemy)

# Move between patrol points and call the function.

while True:  
hero.moveXY(37, 34)  
# Use cleaveOrAttack function you defined above.  
cleaveOrAttack()

```
hero.moveXY(47, 30)
# Call the function again.
cleaveOrAttack() 
        
hero.moveXY(58,31)
# Call the function again.
cleaveOrAttack()`

```

Error Message highlights hero.attack(enemy) in red and Reads: Fix Your Code. Line 12: attack’s argument target should have type object, but got null. Target is null. Is there always a Target to attack? (Use if?)

My apologies for my stupidy and thank you for your patience.

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 12, 2018, 5:13pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/17 "2018-04-12T17:13:24Z")

</div>

The problem is in your function but I can’t see the structure because you didn’t post your code correctly.

Also, it looks like there is a white space in the name of the function that wasn’t there before. There shouldn’t be any spaces in the name of the function.

---

<div class="post-metadata">

### Author: ![BlackRabbit](https://avatars.discourse-cdn.com/v4/letter/b/f14d63/32.png) [@BlackRabbit](https://discourse.codecombat.com/u/BlackRabbit)
#### Post date: [April 12, 2018, 5:34pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/18 "2018-04-12T17:34:34Z")

</div>

My aplogies I posted it incorrectly, here it is in the correct format. This is the latest with attempts to debug utilising the say command.  
the first two hero.say correctly show the defined enemy variable, however after the else statement, enemy is null. Can you explain why the enemy is being set to null after the else statement.

```python
def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.say(enemy)
        if hero.isReady("cleave"):
            hero.cleave(enemy)
            hero.say(enemy)
    else:
        hero.say(enemy)
        hero.attack(enemy)

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack()
            
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack() 
            
    hero.moveXY(60,31)
    # Call the function again.
    cleaveOrAttack()

    hero.moveXY(61,30)
    # Call the function again.
    cleaveOrAttack()

```

---

<div class="post-metadata">

### Author: ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)
#### Post date: [April 12, 2018, 5:51pm UTC](https://discourse.codecombat.com/t/solved-village-champion-issues/13952/19 "2018-04-12T17:51:53Z")

</div>

Look closely at the structure of your function. The entire problem is there. As I pointed out before (see above example), the else conditional is not aligned properly. That’s why it’s failing.
