Python coding question


#1

I understand this is a game site, but is this code really python code? Will this actually help me learn to code other programs not related to games, like web development?


#2

It’s real Python. Programming consists of multiple skills. CodeCombat teaches some of them.

Without knowing about your background in programming it’s impossible to say how much you will learn via CodeCombat.


#3

I’m not a novice or expert, I’m somewhat intermediate. I just began this site, the “loop” cmd or function, I have never seen it used this way:

loop:
    self.moveUp()

This is an infinite loop I believe. I’m use to seeing for and while loops. I’m curious to know why the for loop was not introduced until far later on in learning?


#4

Yes, that is technically an infinite loop.

At code combat there are two kinds of infinite loops…

Those that run forever WITHOUT allowing the whole system to function properly (the bad kind)
and those that run forever but DO let the rest of the system function properly (the good kind).

“while True:” would be an example an example of the bad kind. (sorry, if python is true and not True, I get confused as to which languages are capital booleans and which are small, hurray for editor syntax color/highlighting)

“loop:” is a command added to python (and javascript?) to allow for the good kind.

(one can turn the first type into the happy kind by making sure your hero does an action (say, move nowhere, “self.moveXY(<insert hero’s current x & y coord here>)”) every (couple of) time(s) around the loop…

I believe it is introduced first, so people can get a handle on other things before they have to start worrying about accidentally forgetting to “increment/decrement/change” their loop control variables (and achieving the “good” kind of loop without changing how while/for work.) (We need this for all the languages at code combat.)


#5

In the case of self.moveUp() I don’t think that there a difference between the while true and loop. The hero needs a certain time to complete the moveUp() and after that’s completed the next command get’s executed.

If you however write self.attack(enemy) there’s a difference. That commands needs no time to execute. It just tells you hero that he has to attack the enemy and the next line of the code get’s executed immediately.

As a result if you write

while True: 
    self.attack(enemy)

you get an infinitive loop. The code throw an error.

You don’t get an error if you write

loop: 
    self.attack(enemy)

Loop is a command that was added in this game to make things easier for beginners.

It does something like:

while True: 
    self.attack(enemy)
    waitForNextFrame()

Having to think about calling wait makes things more complicated for beginners and CodeCombat wants to spare the beginner from dealing with that at the beginning.

@Vlevo: That explanation is more confusing then helpful if someone wants to use Python outside of CodeCombat. Python has no loop: command. That’s something that CodeCombat adds.


#6

I did say it was added.

You may feel yours is easier to understand (and it may be) but it contains nothing that was not in mine. So please if you wish to make snide remarks put that part in a private message.

attack does take time. if there is no enemy it will error – null enemy (not inf loop), if there is an enemy you will either move (taking time) or attack which also takes time. So please…


#7

There is a bit of editing done in the API to make the code more appropriate for the game.


#8

Hi,

Any condensed syntax for repeating lines but not using any additional loop for a statement like this:

if len(enemies) != 0 or len(enemies) != 1 or len(enemies) != 2:
some function

Can it be condensed to:
if len(enemies) != {0, 1, 2}:
same function

?


#9

Don’t forget to format your code properly.

First of all, your two pieces of code are not equal. The first one will be true if any of the requirements are true. The second will come true if none of them are. The second one should probably look like this:

if len(enemies) not in [0,1,2]:
    # Do stuff. 

I’m not sure if you can condense the first.


#10

Addressing your first question – I’m new to coding. Took a one semester VB course in high school some 15 years ago. I started looking at Python tutorials on the web. Didn’t get too far. After getting relatively far into this game I recently went back to one of the Python tutorials and although I obviously can’t sit down in front of Notepad++ and write software just because I made cutesy medieval creatures dance in a carefully constructed padded-wall prison cell of a programming interface, those other tutorials make a lot more sense now. I can read the Python syntax more like a coherent sentence. So look at it that way.