Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Does CodeCombat use "For" loops?

For example:

enemies = hero.findEnemies()
for enemy in enemies:
    hero.attack(enemy)

instead of:

enemies = hero.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies)
    enemy = enemies[enemyIndex]
    hero.attack(enemy)
    enemyIndex += 1

you can see that one is shorter and simpler than the other… right?

Yes, it does…it is granted with Programmaticon IV. However, you can use it before acquiring IV.

ok

why doesn’t it explain for loops before hand instead of waiting so long?
IMO, while loops to do this is kind of complicated

Do a search on ‘programmaticon iv’ and you’ll find a good explanation from Nick and team. To paraphrase, CoCo is set up for progressive learning of the concepts, from basic to relatively complex. The intent is that you have a grasp of the concept, before taking on another.

For those who have a background in coding, or are otherwise familiar, this can be kind of tedious…I am one of those. However, I also understand the importance of using the methods employed here, to teach those who are starting from scratch.

My advice is: if you know how to do it elsewhere (in another language), try it here. If unable to get it to work, or need a nudge in the right direction (as I did, since I’d never heard of Python prior to starting 2 months ago), then certainly pose the question on the forum.

1 Like

To add to what Dedreous said, the reason they teach you while loops is because they can do things that for-loops can’t.
In the example you gave, a for-loop would be the best option. But that’s only one type of looping. What if you wanted to loop while a certain condition was true?
This is a program to monitor the temperature in a factory making sure it’s within a safe limit.

temp = input(temperature) # from a sensor
while temp < 30 and temp > 0: 
    print("The temperature is safe.") 
    temp = input(temperature) 
print("ALARM SOUNDING!!")
# This is an example of a program which could not be achieved using a for-loop.

Although using while loops to do things better suited to a for-loop is annoying, it’s important to know how to use them, because they’re very helpful for other things.
Danny

3 Likes

To add also to what @Deadpool198, Danny said,

In my classes, students often do not appreciate the technology and development that we have. The “for-loop” and while-loop are actually also technological advancements. With higher developed languages like JS AND Python, we do not even have to tell the computer to start a loop, and then loop back to the start until a condition is met. It is absolutely FASCINATING to see how humanity has advanced.

Another math example would be exponents. We could simply multiply 222222, etc., but it is MUCH quicker to write 2^6. In a similar way, it would be easy to write 48 instead of 4+4+4+4+4+4+4+4. Finally, if you have every taken calculus, it is much easier to use the derivative rules. However, students do not appreciate (and most times then do not remember) those rules, if they were not first taught how work with limits.

1 Like

Very apt! (20 lines of)

Deadpool code is close to the “do while” loop that is missing in python


Another way of doing it is:

1 Like

The “do while” loop here that you said, is NOT missing in python. In the cloundrip mountain of codecombat, it will be explained and labelled as “advanced while loops”. So it is actually always there, and you can use it anytime as long as you understand it

Ok, thanks for the info.

For those curious: Emulate a do-while loop in Python?