[SOLVED] For CodeCombat Teachers: Timberland Trap


#1

I’m on a concept challenge called Timberland Trap. I’m pretty sure that only those who have a classroom know this challenge. I’m having trouble finding out how to make a function that accepts a parameter. The following is the directions: Define a function that allows a parameter. What I don’t understand is how to make a function that takes a parameter. If you can help that would be great, thank you!


#2

A function that takes a parameter is pretty much when you have something inside the brackets of the function, e.g.

def findIfNotThrower(enemy):
    #your function

If you have any more trouble with this level please post your code.
Thanks :lion: :lion: :lion:


#3

Ok, thanks! :+1:t2: I will if I do have any problems.


#4

I’m having trouble getting my code to work
21%20PM


#5

The function needs to be outside the while True loop and you can’t have an else or elif conditional statement unless there’s an if conditional statement preceding it.


#6

oh… I did the opposite. Thank you.


#7

I have a question, do you understand what they are trying to say in the directions and hints? Directions:

Define a function that accepts a parameter.

Defeat munchkins, ignore throwers.

Now the hints:
This is a concept challenge: Define a function with a parameter.
For this level, you need to do the following:

  1. Define your own function that accepts a parameter.
  2. Defeat all the munchkins, while ignoring the throwers.
  3. Use cleave whenever you can, to make sure munchkins don’t swarm you in large groups.
    Then last hint:
    The easiest way to make sure you don’t attack the throwers is to check that the distanceTo your enemy is less than some amount.
    Can you tell me what they’re are trying to explain so I can get a clearer view of what to do?

#8

What they mean is that you have to define a function like this:

So you need to actually define it like Munkey said, outside the loop.
As for the

You need to put something in the brackets.
So look at this code:

def findAllWhoAreClose(enemies): #this function will return an array of close enemies.
    close = [] #making the array
    for enemy in enemies: #iterating through the enemies
        #execute your code using hero.distanceTo() or enemy.type()
    return close #returning the array

while True:
    enemies = hero.findEnemies()
    inRangeEnemies = findAllWhoAreClose(enemies) #implementing your function
    nearestInRange = hero.findNearest(inRangeEnemies) #turning you array into one object
    if nearestInRange  #......Execute the rest of your code

This is just a simple function using a parameter ( the enemies in the brackets).
Have you worked with functions before?
Because there are some really good levels in the mountain which explain how to make your own. You do see them before in pre-written code but those are the first levels where it properly explains it.

Another format of code with a function and a parameter (which is a bit more simple):

def findIfNearOrRightType(enemy): #there's only one enemy, this function will return either True or False
    if #execute code using hero.distanceTo()
        return True
    else return False

while True:
    enemy = hero.findNearestEnemy() #finding your nearestEnemy()
    if enemy and findIfNearOrRightType(enemy): #this can be used like a condition, if the returned statement is true, it will execute the code inside the if statement, if not it will carry on.
        #finish of your enemy
    

I hope this helps and I haven’t said too much, I just wanted to give you a thorough explanation of functions.
:lion: :lion: :lion: