# \[SOLVED\] Help! Toil and Trouble

**URL:** https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690
**Category:** Level Help
**Created:** [October 9, 2020, 4:51am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690 "2020-10-09T04:51:46Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)
#### Post date: [October 9, 2020, 4:51am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/1 "2020-10-09T04:51:46Z")

</div>

> **Code**
>
> ```python
> def chooseTarget(friend):
> friends = hero.findFriends()
> for friend in friends:
>         
> if friend.type == "soldier":
> return "attack-witch"
> elif friend.type == "archer":
> return "attack-nearest"
> 
> def attackWitch():
> soldiers = hero.findByType("soldier")
> for soldier in soldiers:
> witch = soldier.findNearest(hero.findByType("witch"))
> if witch:
> hero.command(soldier, "attack", witch)
> 
> def attackNearest():
> archers = hero.findByType("archer")
> for archer in archers:
> enemy = archer.findNearest(archer.findEnemies())
> hero.command(archer, "attack", enemy)
> 
> while True:
> friends = hero.findFriends()
> for friend in friends:
> # Use your chooseTarget function to decide what to attack.
> target = chooseTarget(friend)
> if target == "attack-witch":
> attackWitch()
> elif target == "attack-nearest":
> attackNearest()
> pass
> 
> ```

> **Error**
>
> ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/018551ef7123058c90782a8bfb6fc5631446e128.png)  
> Everyone dies before all the ogres are dead. There were 2 ogres left when everyone died.

> **Link**
>
> [https://codecombat.com/play/level/toil-and-trouble](https://codecombat.com/play/level/toil-and-trouble)?

```python
Lydia

```

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [October 9, 2020, 7:25am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/2 "2020-10-09T07:25:21Z")

</div>

Here’s something to get you started. This level only really takes 7 lines to beat it **legitimately.**  
But I don’t want to make things too confusing, so we’ll go with the long method.

Step 1: Change the `attackWitch` function:  
Right now your `attackWitch` function doesn’t take any parameters, but here’s the catch – You can take an argument “friend”. So instead of having just `def attackWitch()`, you can do `def attackWitch(friend)`. This will save you **lines** of code.

Step 2: Edit the inside of the **new** `attackWitch` function:  
First, remove the `soldiers = hero.findByType("soldier")` line, you won’t need it because we’ll have it included in the arguments Next, remove the `for loop` as it’s now obsolete to have – As I said before, you already received a unit to command. In the `witch = soldier.findNearest(hero.findByType("witch"))` line, replace `soldier` with whatever you named the argument in the function `attackWitch(argument)`. After that, command that unit to attack the witch.

Step 3: Remove attackNearest():  
Remove the `attackNearest()` function since it’s unneeded.

Step 4: Remove chooseTarget()  
As above, it’s obsolete to have it, so feel free to delete it.

Step 5: Edit the while True loop. Part A:  
Keep the `findFriends()` and also keep the `for-loop`, but instead of getting the target, check if the type of the `friend` is an archer. If so, then get the `friend.findNearestEnemy()` for the archer, and have them attack it using the `hero.command()` function.

Step 6: Edit the while True loop. Part B:  
Next, check if the `friend.type` is `soldier`, if so, then run our `attackWitch` function with the argument `friend` in it, so we’ll have `attackWitch(friend)`.

If you do the above steps, then you should be able to finish the level 👍

 ![Screen Shot 2020-10-09 at 4.23.53 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/828343f54176bb3a0f60cc4ceef4f471820596dc.jpeg)

> **7-line of code proof:**
>
> ![Screen Shot 2020-10-09 at 4.28.16 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/5/5674f837703ee86691fa9cc41ddff0a130ac6ab4.jpeg)

---

<div class="post-metadata">

### Author: ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)
#### Post date: [October 9, 2020, 9:17am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/3 "2020-10-09T09:17:40Z")

</div>

Has anyone managed to pass the level without casualties?  
Two years ago, I lost a lot of time trying to do it with a free hero, but I couldn’t…

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [October 9, 2020, 9:18am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/4 "2020-10-09T09:18:16Z")

</div>

@xython would using a subscriber hero count? 🙂

---

<div class="post-metadata">

### Author: ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)
#### Post date: [October 9, 2020, 9:21am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/5 "2020-10-09T09:21:49Z")

</div>

My main is a non subscriber, but I switch often to my son’s subscriber account.

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [October 9, 2020, 9:29am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/6 "2020-10-09T09:29:15Z")

</div>

@xython there we go! No casualties! 🙂

 ![Screen Shot 2020-10-09 at 6.28.38 PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/7/7d399729874298e111b2cfb6fd18853c2bb9ea1e.png)

---

<div class="post-metadata">

### Author: ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)
#### Post date: [October 9, 2020, 9:35am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/7 "2020-10-09T09:35:13Z")

</div>

Oh yes, I figured out how to do it with a wizard! Thank you!

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/b/b75793519ac2d01e69f5258597c330a4fc177607.jpeg)  
I had to retreat one of my soldiers, he got a little bit more damage

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)
#### Post date: [October 9, 2020, 9:37am UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/8 "2020-10-09T09:37:43Z")

</div>

No problem! Thanks for the challenge. 🤩 It’s pretty fun going over previous levels and aiming for the scoreboard or optimizing the code. Giving yourself “extensions” to aim for is definitely a good habit. (And helps you learn)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)
#### Post date: [October 9, 2020, 11:27pm UTC](https://discourse.codecombat.com/t/solved-help-toil-and-trouble/24690/10 "2020-10-09T23:27:09Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
