Python (civ4 scripting language) tutorial thread

:(

I have to apologize for not attending classes...

I am currently finishing my project for my study and just dont have any time until late oktobre.

On 8 oktobre my 'essay' has to be done. :crazyeye: :rolleyes: :eek: :mad: :sad: :confused: :cool:
 
Welcome to CanuckSoldier (and any others I have forgotten to say hi to)!

I'm writing the new lesson now, though I dont know when it will be out.

EDIT - MMAfan - Dont worry, just post here when you have caught up. I understand your problem, I'm in the middle of quite a few assesments.
 
Ok, the new lesson has been written. I'll have to post it tomorrow, there's too much organisation to set the next lesson up tonight.

Until then, remember to post your progress in the lesson! There are many people who haven't yet.
 
LESSON 4:
Loops, Loops, Loops, Loops, Loops, Loops, Loops...

Ahh, our final lesson before we get into interacting with human input. Can't wait, can you?

Just imagine you needed a program to do something 20 times. What would you do? You could copy and paste the code 20 times, and have a virtually unreadable program, not to mention slow and pointless.

Or, you could tell the computer to repeat a bit of code between point A and point B, until the time comes that you need it to stop. The folling are examples of loops in Python:
Code:
a = 0
while a < 10:
    a = a + 1
    print a
How does this program work? Lets go through it in English:
Code:
'a' now equals 0
As long as 'a' is less than 10, do the following:
    Make 'a' one larger than what it already is.
    print on-screen what 'a' is now worth.
What does this do? Lets go through what the computer would be 'thinking':
a = 0
Code:
#[b]JUST GLANCE OVER THIS QUICKLY[/b]
#[b](It looks fancy, but is really simple)[/b]
Is 'a' less than 10? YES (its 0)
Make 'a' one larger (now 1)
print on-screen what 'a' is (1)

Is 'a' less than 10? YES (its 1)
Make 'a' one larger (now 2)
print on-screen what 'a' is (2)

Is 'a' less than 10? YES (its 2)
Make 'a' one larger (now 3)
print on-screen what 'a' is (3)

Is 'a' less than 10? YES (its 3)
Make 'a' one larger (now 4)
print on-screen what 'a' is (4)

Is 'a' less than 10? YES (its 4)
Make 'a' one larger (now 5)
print on-screen what 'a' is (5)

Is 'a' less than 10? YES (its 5)
Make 'a' one larger (now 6)
print on-screen what 'a' is (6)

Is 'a' less than 10? YES (its 6)
Make 'a' one larger (now 7)
print on-screen what 'a' is (7)

Is 'a' less than 10? YES (its the meaning of life)
Make 'a' one larger (now 8)
print on-screen what 'a' is (8)

Is 'a' less than 10? YES (its 8)
Make 'a' one larger (now 9)
print on-screen what 'a' is (9)

Is 'a' less than 10? YES (its 9)
Make 'a' one larger (now 10)
print on-screen what 'a' is (10)

Is 'a' less than 10? [b]NO[/b] (its 10, therefore isn't [b]less than[/b] 10)
Don-t do the loop
There's no code left to do, so the program ends
So in short, try to think of it that way when you write 'while' loops. This is how you write them, by the way (and a couple of examples:
Code:
while {condition that the loop continues}:
    {what to do in the loop}
    {have it indented, maybe four spaces}
{code that is not looped}
{don't indent this stuff}

#EXAMPLE
#Type this in, see what it does
x = 10
while x != 0:
    print x
    x = x - 1
print "wow, we've counted x down, and now it equals", x
Remember, to make a program, you open IDLE, click File > New Window, type your program in the new window, then press F5 to run.

What do you type in the area marked {conditions that the loop continues}? The answer is a boolean expression.

WHOA! Big word for the non-math people here. Never mind, boolean expression just means a question that can be answered with a yes or no response. For example, if you wanted to say your age is the same as the person next to you, you would type:

My age == the age of the person next to me

And the statement would be TRUE.
If you were younger than the person opposite, you'd say:

My age < the age of the person opposite me

And the statement would be TRUE.
However, if you were to say the following:

My age > the age of the person opposite me

The statement would be FALSE - the person is actually younger than you.
Here are all of the boolean signs (or operators):
Code:
Expression   Function
<            Less than
<=           Less than or equal to
>            Greater than
>=           Greater than or equal to
==           Equal to
!=           Not equal to
<>           Another way of saying not equal to
Dont get '=' and '==' mixed up - the '=' operator makes what is on the left equal to what is on the right. the '==' operator says whether the thing on the left is the same as what is on the right.

If the result of the boolean question is true, the loop is run.

OK! We've (hopefully) covered 'while' loops. Now let's look at something a little different - conditionals.

Conditionals are where a section of code is only run if certain conditions are met. This is similar to the 'while' loop you just wrote, which only runs when x doesn't equal 0. However, Conditionals are only run once.

The most common conditional in any program language, is the 'if' statement. Here is how it works:
Code:
if {conditions to be met}:
    {do this}
    {and this}
    {[i]and[/i] this}
{but not this, because it isn't indented}

#EXAMPLE 1
y = 1
if y == 1:
    print "y still equals 1, I was just checking"
    
#EXAMPLE 2
print "We will show the odd numbers up to 20"
n = 1
while n <= 20:
    if n % 2 == 0:
        print n
    n = n + 1
print "there, done."
Example 2 there looks tricky. But all we have done is run an 'if' statement every time the 'while' loop runs. Remember that the % just means the remainder from a division - just checking that there is nothing left over if the number is divided by two - showing it is even. If it is even, it prints what 'n' is.

Finally, there are more ways of doing the 'if' statement. They are 'else' and 'elif'.

'else' simply tells the computer what to do if the conditions of 'if' arent met. For example, the following:
Code:
a = 1
if a > 5:
    print "That's weird"
else:
    print "That's normal"
'a' is not greater than five, therefore what is under 'else' is done.

'elif' is just a shortened way of saying 'else if'. When the 'if' statement fails to be true, 'elif' will do what is under it IF the conditions are met. For example:
Code:
z = 4
if z > 70:
    print "Something is very wrong"
elif z < 7:
    print "This is normal"
The 'if' statement, along with 'else' and 'elif' follow this form:
Code:
if {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
else:
    {run this code}

#You can have as many or as little elif statements as you need
#anywhere from zero to the sky.
#You can have at most one else statement
#and only after all other ifs and elifs.
One of the most important points to remember is that you MUST have a colon : at the end of every line with an 'if', 'elif', 'else' or 'while' in it. I forgot that, and as a result a stack of people got stumped at this lesson (sorry ;) ).

One other point is that the code to be executed if the conditions are met, MUST BE INDENTED. That means that if you want to loop the next five lines with a 'while' loop, you must put a set number of spaces at the beginning of each of the next five lines. Here is an example of both of the above points:
Code:
a = 10

while a > 0:
    print a
    if a > 5:
        print "Big number!"
    elif a % 2 != 0:
        print "This is an odd number"
        print "It isn't greater than five, either"
    else:
        print "this number isn't greater than 5"
        print "nor is it odd"
        print "feeling special?"
    a = a - 1
    print "we just made 'a' one less than what it was!"
    print "and unless a is not greater than 0, we'll do the loop again."
print "well, it seems as if 'a' is now no bigger than 0!"
print "the loop is now over, and without furthur adue, so is this program!"
Notice the three levels of indents there:

1) Each line in the first level starts with no spaces. It is the main program, and will always execute.

2) Each line in the second level starts with four spaces. When there is an 'if' or loop on the first level, everything on the second level after that will be looped/'ifed', until a new line starts back on the first level again.

3) Each line in the third level starts with eight spaces. When there is an 'if' or loop on the second level, everything on the third level after that will be looped/'ifed', until a new line starts back on the second level again.

4) This goes on infinitely, until the person writing the program has an internal brain explosion, and cannot understand anything he/she has written.

There is another loop, called the 'for' loop, but we will cover that in a later lesson.

You'll probably have to ask a lot of questions about this lesson - I don't know how clearly I have written it. Please, if something is unclear, tell me.

And that is lesson 4! In lesson 5, we get into user interaction, and writing programs that actually serve a purpose. Can't wait!

Thanks to all, Gingerbread Man.
 
Is it to late to join up in this? I have virtualy no knowlage of programing
 
MSTK said:
I already knew lesson 3 & 4, and they are self explanitory if you know any programming language at all :(

Oh well. It's still fun :)
I probhibly need help catching up. Place me as still going on Lesson 1 ATM.
 
Edit: Apperently I have gotten this thingy to work :wallbash:

I would like to say that I am finished with lessons 1-4. Why am I getting the feeling that this is a review from Algebra class :hmm:

Also, I have been around with basic logics (And, Or, Not, Nor, etc) from electrionics class in High School.
 
okay i had problems with this lesson:

the last two examples i typed them in and pressed F5 and it said there was a synastic (spel?) error. Then i copied both and it gave the same error. can someone help me:confused:. other than that i finished the rest of the lesson.
 
I'm getting errors too...

Blackbird - I think that's a syntax error, it's what I'm getting as well.

hurrah for 3 edits. Found what was wrong, you've gotta have a colon after the if condition and the elif/else's.
 
Trump,
yeah its a syntax error. i tried what you said but it still said ivalid syntax. Here is what it typed in:

a = 1
if: a > 5
print "That's weird"
else:
print "That's normal"

EDIT: I found out the problem. I put the colons in the wrong places. This is what I should have typed in:

a = 1
if a > 5:
print "That's weird"
else:
print "That's normal"

Thanks Trump for helping me. :D
 
Sign me up :D
I've completed all lessons although I'm a bit fuzzy on "elif" ;)

I have a question though, why wont this work?
Code:
z = 4*20
if z-11 > 70:
    print "Something is very wrong"
elif z-11 < 7:
    print "This is normal"
 
Code:
z = 4*20
if z-11 > 70:
    print "Something is very wrong"
elif z-11 < 7:
    print "This is normal"
The above code works as it is writen. Since "z - 11"( z = 4*20 = 80, 80 - 11 = 69) isn't above 70 nor below 7 the program will not print anything.
Though I guess that the "7" in the elif-function is a typo and is supposed to be "70". If corrected, the program works.
 
I am done.
 
I knew this would be a toughie!

Welcome aboard CivGeneral and Dease! Everybody is welcome to join.

Great to see you guys discussing the lesson - you even figured out the answer to your own question. Yes, you do have to remember those colons at the end of the 'if's.

And thankyou! You just pointed out a stack of typos!
 
Gingerbread Man said:
LESSON 4:
Loops, Loops, Loops, Loops, Loops, Loops, Loops...


Code:
Expression   Function
<            Less than
<=           Less than or equal to
>            Greater than
>=           Greater than or equal to
==           Equal to
!=           Not equal to
<>           Another way of saying not equal to


#EXAMPLE 1
y = 1
if y = 1:
    print "y still equals 1, I was just checking"

Seems like you have a typo in the #EXAMPLE 1

I think it must be:
Code:
#EXAMPLE 1
y = 1
[color=red]if y == 1: [/color]
    print "y still equals 1, I was just checking"

I finished lesson 4. :banana:
 
Top Bottom