Modding help

First of all, use the "code" tags (shortcut as "#" in the composition panel) when you post code. Otherwise the whitespace don't show and it becomes very hard to follow any Python you post.

The Traceback thing is Python's way of delivering error messages. You will get those in CivIV Python also, so you might as well know this now.

Once you get a error message the interpreter will show a "traceback", from the original call in main and through any modules and functions you have called. That way you can actually follow the entire process and - hopefully - realize what the problem is.

So there is no Traceback command or statement in Python - the error message is the trace, back to where you initiated the code. Just do what the textbook says in order to produce the traceback in the example, and it should become apparent to you.

Don't worry about it if you don't fully understand this now, you can revisit it when you read up more on trouble shooting. :p

ps You managed to get Bruce to eat his Spam alright? :D
 
The book does describe what functions are but I imagine the whole concept can be a bit strange at first. If your thought process isn't following along, then the examples (with the Bruce and the Spam) might just serve to confuse you. So, here's my take on the subject.

Instead of writing a statement like:
Code:
print 5 * 5
...you could make it into a function:
Code:
def powerOfFive():
    print 5 * 5
What the function definition does is it makes the content (the body) of the function be the name of the function. So when you call on the function, like this:
Code:
powerOfFive()
...the interpreter only recognizes the code inside the function, which would be:
Code:
print 5 * 5
So, you basically trick the interpreter to run some code by calling upon a definition. Since you defined the function as that code. Almost like when you set a variable:
Code:
powerOfFive = print 5 * 5
The function above isn't a very useful one, but that is beside the point. :p

This would be a more useful function:
Code:
def powerOf(value):
    print value * value
This function takes an argument, the variable "value". You use it like this:
Code:
powerOf(5)
The point would be that you can pass any value to the function when you call upon it. Another variant would be:
Code:
def powerOf(value):
    return value * value
Since the Print command isn't included you would use it like this:
Code:
print powerOf(5)
The feature would be that you can use the function for other things than printing the result into the interpreter. Like use it as a part of a CivIV mod. :D

edit: As trivia, the return statement is optional in any function, but can be very useful nonetheless. If a function doesn't have a return statement, then it returns the value None/Null, which basically means "nothing". But regardless, a function will always return something, even if it isn't anything.
 
Thanks :)
Btw your "def powerOf(5) :
#return value*value" didnt work... it says that the (5) is a synthax error... havent figured out how to open python in civ yet....

Edited: the powerOf(value) works, but it says:
25
none

where does the none come from?
 
Btw your "def powerOf(5) :
#return value*value" didnt work... it says that the (5) is a synthax error... havent figured out how to open python in civ yet....

Edited: the powerOf(value) works, but it says:
25
none

where does the none come from?
I don't believe you are using the interpreter correctly. This is how you do it:
PHP:
>>> def powerOf(value): #function definition with argument
	    return value * value #body of function containing code
	
>>> print powerOf(5) #print statement with a function call
25
Note the new command line (">>>") for the actual function call.

edit: I wrote the function definition (with a body containing the code) on the first command line. Then I called upon the function on the second command line. The function call isn't a part of the function, but is used to access the function. There is a difference here that you need to recognize.
 
Here's an example that uses variables for some additional clarity:
PHP:
>>> def powerOf(value):
        return value * value
    
>>> originalValue = 5
>>> newValue = powerOf(originalValue)
>>> print newValue
25
The first variable (originalValue) could be used in a Python module for a CivIV mod, and the powerOf() function could be a definition in a mod. Instead of printing out the result, the second variable (newValue) could be used to, say, determine how much :gold: is granted to a spawning Civ. Or whatever. Then you would replace the print statement with something like:
Code:
CyPlayer.setGold(newValue)
Note how CivIV Python uses dot notation - this will be covered in detail in the book. By the way, "CyPlayer" is an "object" corresponding to a actual Civ and by using this code your would actually be doing Object-Oriented Programming. :king: Since you put a object in front of a function (or method really) separated by a dot. Hence dot notation and object oriented.

It will all be clear to you eventually. :king: If you ever find the built in interpreter we could do some CivIV Python exercises like this to whet your appetite.

ps I posted a private message also.
 
it worked... now im stuck in another problem:
4.5 Alternative execution

A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:

if x%2 == 0:
print x, "is even"
else:
print x, "is odd"

If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. If the condition is false, the second set of statements is executed. Since the condition must be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.

As an aside, if you need to check the parity (evenness or oddness) of numbers often, you might "wrap" this code in a function:

def printParity(x):
if x%2 == 0:
print x, "is even"
else:
print x, "is odd"
i did this:
x= 5
def printParity(x):
#if x%2==0:
##print x, "is even"
#else:
##print x, "is odd"
printParity(x)

then a synthax error :/

Succes:) ive gone through 1.5 chapter today :)
 
Succes:) ive gone through 1.5 chapter today :)
Well done! :goodjob:

Just a reflection: You seem to still be struggling with using the interpreter, but you're not even required to know how one works in order to mod CivIV... (Instead, you would be scripting modules - the console is more of a cheat feature anyway.) So it feels a bit like you're focusing on the wrong thing here.

I can see how you wanna successfully replicate all the examples in the textbook, but you could just read them and trust that they would work. (The book touches on the issue of a "leap of faith".) It shouldn't hurt to try them out for yourself, but I'm not entirely sure what good it will do either - at least if you don't understand what the code is actually doing.

If you feel that you need to get hands-on and play around with code while reading, you could always do the exercises at the end of each chapter instead. Because that would require you to use what you just learned and solve a specific problem. If you can't - chances are that you shouldn't skip to the next chapter quite yet...

We could do the exercises together, by the way, since I never bothered to use a interpreter or do any code while reading (and I read the whole thing twice, more or less). So copy-paste a exercise you wanna try and attach your best attempt. If you can't quite figure it out I could give it a try myself (and anyone else shadowing this thread) and post a solution inside spoiler tags. :king:

Looking back at the code I posted earlier in this thread, I realize that the function used really should be called "squared()" instead. A function called "powerOf" should in fact look something like this and yield completely different results.
Code:
def powerOf(value):
    return value ** value
 
i cant understand this, did exactly what it says... how do i import a math module?

The return statement allows you to terminate the execution of a function before you reach the end. One reason to use it is if you detect an error condition:

import math

def printLogarithm(x):
if x <= 0:
print "Positive numbers only, please."
return

result = math.log(x)
print "The log of x is", result

The function printLogarithm has a parameter named x. The first thing it does is check whether x is less than or equal to 0, in which case it displays an error message and then uses return to exit the function. The flow of execution immediately returns to the caller, and the remaining lines of the function are not executed.

Remember that to use a function from the math module, you have to import it.
 
when i tried your
def powerOf(value):
return value ** value

it just showed an empty line
If you only type in the definition (the code you posted above) it does nothing - unless you call upon it. Like this:
Code:
powerOf(5)
Then the function call results the value 3125. But it still doesn't print anything out, so you could do:
Code:
print powerOf(5)
or even:
Code:
x = 5 #assignment statement
y = powerOf(x) #assignment statement with function call
print y #print statement
This tells me that you haven't quite figured out how to use the interpreter yet. Maybe we should start from the beginning with some easy-to-follow exercises?
 
i cant understand this, did exactly what it says... how do i import a math module?
All the math functions (or methods) that are built into Python reside in a module called math.py - and you import the entire module by typing:
Code:
import math
Do this first, then try to use the math function(s). Otherwise the interpreter will complain about you not defining the function before you call upon it. (You could, of course, code your own function and call upon it instead.)

Trivia: In Rhye's files there are a bunch of import statements at the beginning of every module. If you make your own module and call it, say, Custom.py - then you import it to another module by including the line:
Code:
import Custom
Straight forward enough?
 
Ok... Lets start from the beginning.

Restart IDLE (the Python interpreter). After the introduction text and version number and whatnot there is an empty command promt, like this:
Code:
>>>
Enter this:
Code:
print "Hello World!"
This is what you would see:
Code:
>>> print "Hello World!"
Hello World!
>>>
The first line is the original command promt containing your program (only consisting of a print statement). The second line is the output of your program. (The interpreter will only give you any output if you use the print command. Its not a very useful command for modding, though...) The third line would then be a new command promt.

Now, at the new command promt, enter this:
Code:
def hello(word):
You just entered a definition statement, and this is why there isn't a new command promt - or any output. Because now you're required to enter some code into the body of the function you're defining. So you enter this:
Code:
print "Hello " + word + "!"
This is what you would see on screen:
Code:
>>> print "Hello World!"
Hello World!
>>> def hello(word):
	print "Hello " + word + "!"
Still no new command promt - and no output! This because the interpreter is waiting for you to enter more code into the body of the function. We're not gonna, so just hit the Enter key one more time.

Now you get a fresh command promt. So you enter a function call (followed by Enter):
Code:
hello("World")
This is what you should see:
Code:
>>> print "Hello World!"
Hello World!
>>> def hello(word):
	print "Hello " + word + "!"

>>> hello("World")
Hello World!
>>>
End of lesson #1. I'll continue once you tell me you actually managed this and understood what happened - and why.

Also, you could pass the hello() function any value - like "Baldyr". Like this:
Code:
hello("Baldyr")
If you can't figure out what the output of that function call will be, then try it yourself. :D
 
Top Bottom