Need help...first Boolean map!


#1

Just started my first Boolean map and I am having a little problem. Here is the map link:

https://codecombat.com/play/level/hit-and-freeze?

If you look at the code given to you when you start the board, it says to:

  1. Call inAttackRange(enemy), with the enemy as the argument
    and save the result in the variable canAttack.

and

2)If the result stored in canAttack is True, then attack!

My problems:
A) I am not sure what to do for 1 if someone could explain that in laymen’s terms.
B) How do I write a script that uses the return value of (True and False) as this is the first board to introduce the use of the result of a boolean.

Help is much appreciated on this!


#2

I find breaking it down into simple steps is helpful.

  1. Call inAttackRange(enemy), with the enemy as the argument
    and save the result in the variable canAttack.

How do you define a variable?

variable = something

So, first you want to call the function, inAttackRange(enemy) and then, “save the result in the variable canAttack”.

The name of the variable is canAttack.

canAttack = something

Since we want to call the function, inAttackRange(enemy), and save the result in the variable:

canAttack = inAttackRange(enemy)

The next question is, B) How do I write a script that uses the return value of (True and False)?

Well, we don’t have to worry about writing a script for the return value of False. We’re only concerned with what happens if the return value is True.

If canAttack is True:
    do this

or

if canAttack == True:
    do this

The reason we can omit writing code for the return value of False is because if the value is false, then the code will do nothing, which is what we want. If we wanted to do something when the function returned a value of False, then we would just write exactly as above, but with False.

if canAttack == False:
    do that

or

if canAttack == True:
    do this
else:
    do that

Hope this helps.


#3

Or you could just attack the nearest enemy if within 3m. check via distanceTo(enemy)


#4

That’s what the function does, which is why you’re calling it. The purpose of this level is to introduce calling of a function with a boolean, True or False, output.


#5

I think I understand…but I have a question. When coding the “canAttack” function you follow the name with (). Wouldn’t you also call this function by adding the () at the end of the string name in the “if” statement?

example:

if canAttack() == True:


#6

canAttack is a variable, not a function. Parenthesis follow a function. You define the variable canAttack as the output of the function, inAttackRange(enemy).


#7

I apologize, I should have worded that differently, when writing:

def canAttack():

you use the parenthesis at the end of the function name. When you write the code to recall the function, don’t you have to use the parenthesis at the end of the function name?


#8

Yes. (20 characters)


#9

Great, thanks for the help MunkeyShynes :slight_smile: