Why the redundancy in setting the canAttack variable on Hit and Freeze?

I’ve solved the Hit and Freeze level in the forest but my only slight confusion is about why we bother to store the inAttackRange(enemy) in the canAttack variable? Before i understood how to store the canAttack variable i ran the code like:

if inAttackRange(enemy) == True:
   hero.attack(enemy)

and it worked. My guess is readability for later on if this was a larger game?

Hi Chris_Lane,

Welcome to the forum!

You give one good possible reason for storing the variable; or if you were using the variable several times then it would be less to type out. Or another possiblity is that I found when I was learning it was sometimes easier to split out a variable to be able to understand what was happening. I still do it sometimes when I have lots of things stacked together with different brackets and full stops - it’s a really useful thing to know about.

You sound as though you’ve done a bit of programming before (or have grasped the basics quickly). CoCo will lead you through one way of completing the level, but it’s good if you’re trying other ways of writing the code, and finding out new things for yourself.

A lot of people seem to have fun with the replayable and multiplayer levels, as it frees them up to be creative with the coding :smile:.

Jenny

Hi Jenny,

Glad to be here! I’m having more fun with this than i could have imagined. Yeah i’ve been reading Automate the Boring Stuff by Al Sweigart and thinking about it a lot since i started a few weeks ago.
Using the variable several times and wanting a shorthand sounds like a useful thing to be able to do.
I haven’t come across these multiplayer levels yet, need to explore around a bit more!

Thanks again,
Chris

Hi Chris,
Thanks for the head’s up on the book - I hadn’t seen it before, and it looks great. I’ve just ordered it!
Hope to see you around on the forum from time to time, and happy coding.
Cheers,
Jenny

Hey Jenny,
No probs! Good luck with it.
See you around then!
Chris