Python (civ4 scripting language) tutorial thread

I_pity_the_fool said:
Well, before you do, Python presumably has some kind of module to access the XML files, yes? Ought we not to learn about that, if you could spare time to put something together?
Python certainly does have modules to access XML (known as parsers). The problem is that there are two different types! There's the event based one (E.g. do this when this is found), and then there's the heirachial one (like the folders on your computers). Without knowing which approach is taken, to write a lesson for one method could prove a waste of time if the other method is used.

Graphical User Interface (GUI) programming, however, generally shares many concepts, regardless of how it is implemented. I feel it would be less prone to wasted effort and time. If somebody knows some more details on how civIV will work with XML, I'd be happy to write something about it.
 
I'm just looking for a little clarification in this "for" loop(from lesson 7):

for entry in list:

Okay so "for" and "in" always have to be part of every specific loop you write like this, right?

So really I'm just wondering what exactly "entry" and "list" do and what else you could possibly substitute for them to make the "for" loop do something different then arrange, if thats possible?

If someone could clarify this for me that would be great thanks.
 
Python said:
I'm just looking for a little clarification in this "for" loop(from lesson 7):

for entry in list:

Okay so "for" and "in" always have to be part of every "for" loop you write, right?

So really I'm just wondering what exactly "entry" and "list" do?

If someone could clarify this for me that would be great thanks.

Entry means it will step through every Entry in the List, from 0 to X, where X is the last Entry in the List.

For example this list; List = [1, 67, 33, 90, 5, 7]

Every item has an index;

0:1
1:67
2:33
3:90
4:5
5:7

And in the first Lap of the for-loop it will go to index 0, next run to index 1, etc up to the last index which is index 5.

Atleast thats how I think it works.
 
Okay, so does it look for the first list it see's after it? What I mean is the list doesn't have to be called "list" does it?

e.g.

for entry in list:

(code in between)

countries = ["Canada", "USA", "Mexico"]

or does it have to be

list = ["Canada", "USA", "Mexico"]

for it to recognize what list its sorting out?
 
You write it:

countries = ["Canada", "USA", "Mexico"]

for entry in countries:

....nation = countries[entry]
....print nation


and it will print:

Canada
USA
Mexico
 
For the for loop, the only required words are for and in.
entry can be called anything you want. Example:
Code:
#create a list of some sort:
year = ["jan","feb","mar","april","may","june","july","aug","sept","oct","nov","dec"]
#then put in in a for loop:
for month in year:
    print "month now equals",month
So, as grey fox said, the first time the loop runs, 'month' equals the first item in 'year'. The second time, 'month' equals the second item in 'year', etc. The process is repeated until the loop has been through all items in 'year'.
 
Grey Fox said:
You write it:

countries = ["Canada", "USA", "Mexico"]

for entry in countries:

....nation = countries[entry]
....print nation


and it will print:

Canada
USA
Mexico

Small nitpick, but to avoid confusion, I think it's better to mention: Grey Fox's example should read:
Code:
countries = ["Canada", "USA", "Mexico"]

for entry in countries:

    [I][B]nation = entry[/B][/I]
    print nation
 
grumbler said:
Small nitpick, but to avoid confusion, I think it's better to mention: Grey Fox's example should read:
Code:
countries = ["Canada", "USA", "Mexico"]

for entry in countries:

    [I][B]nation = entry[/B][/I]
    print nation
actually, more concisely:
Code:
for nation in countries:
    print nation
mess around with it, and you'll get the gist.

I'll be gone for the next 10 or so days, on a holiday, so sadly that GUI tutorial will have to wait, but that's not to say it won't be coming.

How long is it now, 'till civ? About a month? This is getting exciting!
 
I've been playing with XML using the Dive into Python book. I now have a simple flashcard program that gets the cards from an XML file.

Right now all it really does is:
1. Can use as many cards as you add.
2. Custimizable labels (for each column)
3. Choose wether you want the first or second column given in the quiz thing.

Later I hope to make it so you can add card within the program so you don't get stuck editing the XML all by your self.

A little note about the XML:
Code:
        <card id="1">
            <Spanish>Hola</Spanish>
            <English>Hi</English>
        </card>
It really doesn't matter what you call the Spanish and English tabs. If you look at the xml file you will see that there is one like this:
Code:
        <card id="4">
            <a>perezoso</a>
            <b>lazy</b>
        </card>
It really doesn't make a diffrance.

Anyway here are the files:

Rename flashcards.txt to .py and card.txt to .xml make sure both files are in the same directory. :)
 

Attachments

Nice job! I'm really liking the lessons and appreciate the effort. However, I got a bit confused in Lesson 5 with this so I wanted to bring it to your attention for a fix:

Code:
#Below is the function
def hello():
    print hello
    return 1234
    
#And here is the function being used
print hello()

Which gave me an error. I *think* you wanted: Print "hello" inside the function, with the quotes.

The following also needs to be changed to When the line "Print hello()" was run... because it looks like you're talking about the 'print hello' command inside the function instead of the line actually calling the function.

So what happened?
1) When the line 'print hello' was run, the function 'hello' was executed.

Thanks again and good job!
 
Do we have a clue about which part of the game we could mod?
Does python gives us a power of changing the game behaviour, or
just changing some parameters... or I may ask how is a python
toll enable us to change the game more than a EDITOR do?( I assume so,
otherwise, why not just give us an editor?)
 
sekong said:
Do we have a clue about which part of the game we could mod?
Does python gives us a power of changing the game behaviour, or
just changing some parameters... or I may ask how is a python
toll enable us to change the game more than a EDITOR do?( I assume so,
otherwise, why not just give us an editor?)

http://www.civfanatics.com/civ4/info/#Customization
 
Really late adopter here. I started last night and finished up through lesson 4, and am most of the way through lesson 5. This really is an excellent tutorial, well written and easy to understand. Thank you!!

My programming knowledge is in C++ (two years in high school, over 5 years since I used it), basic (elementary school!!), a little dabbling in html and something else in elementary called logo, though I remember none of it and am not even sure if that was the real, or commonly used name.

Python seems very much like C++ and basic so far, so I'm having a pretty easy time. We'll see how I progress though.
 
chriseay said:
My programming knowledge is in C++ (two years in high school, over 5 years since I used it), basic (elementary school!!), a little dabbling in html and something else in elementary called logo, though I remember none of it and am not even sure if that was the real, or commonly used name.
:dubious: High school? ELEMENTARY????? I had to wait til college! :mad:

Welcome to CFC!!! [party]
 
Weasel Op said:
:dubious: High school? ELEMENTARY????? I had to wait til college! :mad:

Welcome to CFC!!! [party]

Yeah, elementary school. I was in the gifted program. We did bunches of crazy stuff that most people didn't get to do in school. I actually got to play Civ 1 and Simcity 2000 as projects. A friend of mine made a paper and pencil game loosely based off of civ. It was a great program. Then our high school had both intro, and then AP computer science. No programming in college though, as I have my B.A. in History.

Thanks for the welcome!! :beer:
 
Back
Top Bottom