Upcoming SlideShare
×

# Full Circle 29

1,597
-1

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,597
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Full Circle 29

1. 1. full circle ISSUE #29 - September 2009 TOP FIVE PHYSICS GAMES full circle magazine #29 1 contents ^
2. 2. full circle Program In Python - Pt3 p.07 Ubuntu Games p.23 My Story p.16 MOTU Interview p.20 Read about and anothers ... Command & Conquer p.05 LAMP - Part 2 p.12 Virtual Private Network p.14 Review p.18 Letters p.21 Top 5 p.28 The articles contained in this magazine are released under the Creative Commons Attribution-Share Alike 3.0 Unported license. This means you can adapt, copy, distribute and transmit the articles but only under the following conditions: You must attribute the work to the original author in some way (at least a name, email or URL) and to this magazine by name ('full circle magazine') and the URL www.fullcirclemagazine.org (but not attribute the article(s) in any way that suggests that they endorse you or your use of the work). If you alter, transform, or build upon this work, you must distribute the resulting work under the same, similar or a compatible license. full circle magazine #29 2 contents ^
8. 8. PROGRAM IN PYTHON - PART 3 can re-work the import add them, then statement like this: multiply them, and #silly example then subtract them, print 'Adding the two numbers %d and %d = %d ' % (1,2,1+2) from random import randint print 'Multiplying the two numbers %d and %d = %d ' % (1,2,1*2) displaying the print 'Subtracting the two numbers %d and %d = %d ' % (1,2,1-2) numbers and results print 'n' Now when we call our each time. To make print 'Adding the two numbers %d and %d = %d ' % (1,4,1+4) function, we don't have to use matters worse, we print 'Multiplying the two numbers %d and %d = %d ' % (1,4,1*4) the 'random.' identifier. So, our print 'Subtracting the two numbers %d and %d = %d ' % (1,4,1-4) have to do that three print 'n' code changes to times with three sets print 'Adding the two numbers %d and %d = %d ' % (10,5,10+5) of numbers. Our silly print 'Multiplying the two numbers %d and %d = %d ' % (10,5,10*5) from random import randint print 'Subtracting the two numbers %d and %d = %d ' % (10,5,10-5) example would then # print 14 random integers print 'n' for cntr in range(1,15): look like the text print randint(1,10) shown right. select for the function, and function, we can do it without Functions Not only is this a lot of then a list of parameters (if causing too many issues to our typing, it lends itself to errors, any) in parentheses. This line is main program. We call our When we imported the either by typing or having to then closed by a colon (:). The function, in this case, by using random module, we used the change something later on. code in the function is the function name and putting randint() function. A function is Instead, we are going to create indented. Our improved silly the parameters after. a block of code that is a function called 'DoTwo' that example (#2) is shown below. designed to be called, usually takes the two numbers and Here is another example of more than once, which makes does the math, printing the As you can see, there's a lot a function. Consider the it easier to maintain, and to output each time. We start by less typing involved — 8 lines following requirements. keep us from typing the same using the 'def' key word (which instead of 12 lines. If we need code over and over and over. says that we are going to to change something in our We want to create a As a very general and gross define statement, any time you have the to write the same code more functi #silly example 2...still silly, but better def DoTwo(num1,num2): than once or twice, that code is on). print 'Adding the two numbers %d and %d = %d ' % (num1,num2,num1+num2) a good candidate for a After print 'Multiplying the two numbers %d and %d = %d ' % (num1,num2,num1*num2) 'def' print 'Subtracting the two numbers %d and %d = %d ' % (num1,num2,num1-num2) function. While the following print 'n' two examples are silly, they we make good statements about add DoTwo(1,2) using functions. Let's say we the DoTwo(1,4) wanted to take two numbers, name DoTwo(10,5) we full circle magazine #29 8 contents ^
9. 9. PROGRAM IN PYTHON - PART 3 program that will print out a characters and use “ '=' * could modify the function we list of purchased items in a (width-2)”. To make things just made to include a Let's call the new function pretty format. It must look even easier, we will use parameter for the character to 'Fmt'. We'll pass it 4 parameter something like the text below. variable substitution to put all use in the middle of the pluses. values as follows: these items on one line. So our Let's do that. We can still call it – the value to print on the The cost of each item and string to print would be coded TopOrBottom. left for the total of all items will be as 's ('+',('=' * width-2)),'+'). – the width of this formatted as dollars and cents. Now we could have the routine def “column” TopOrBottom(character,width): The width of the print out must print this directly, but we will – the value to print on the # width is total width be able to be variable. The use the return keyword to send of returned line right (which should be a values on the left and right the generated string back to # character is the floating value) must be variable as well. We our calling line. We'll call our character to be placed – the width of this between the '+' characters will use 3 functions to do this function 'TopOrBottom' and the “column” return '%s%s%s' % task. One prints the top and code for this function looks like ('+',(character * (width- bottom line, one prints the this. 2)),'+') The first task is to format item detail lines including the the information for the right total line and one prints the def TopOrBottom(width): Now, you can see where side. Since we want to format # width is total width separator line. Luckily, there comments come in handy. the value to represent dollars of returned line are a number of things that return '%s%s%s' % Remember, we are returning and cents, we can use a special Python has that will make this ('+',('=' * (width-2)),'+') the generated string, so we function of variable very simple. If you recall, we have to have something to substitution that says, print the printed a string multiplied by 4, We could leave out the receive it back when we make value as a floating point and it returned four copies of comment, but it's nice to be the call to it. Instead of number with n number of the same string. Well we can able to tell at a glance what assigning it to another string, places to the right of the use that to our benefit. To print the parameter 'width' is. To call we'll just print it. Here's the decimal point. The command our top or bottom line we can it, we would say 'print calling line. would be '%2.f'. We will assign take the desired width, TopOrBottom(40)' or whatever this to a variable called 'part2'. subtract two for the two + width we wish the line to be. print TopOrBottom('=',40) So our code line would be Now we have one 'part2 = '%.2f' % val2'. We also function that takes So now, not only have we can use a set of functions '+===============================+' care of two of the taken care of three of the lines, that's built into Python strings '| Item 1 X.XX |' we've reduced the number of lines. We can make a called ljust and rjust. Ljust will '| Item 2 X.XX |' new function to take routines that we need from 3 left justify the string, padding '|-------------------------------|' '| Total X.XX |' care of the separator down to 2. So we only have the the right side with whatever '+===============================+' line using the same center part of the print out to character you want. Rjust does kind of code...OR we deal with. full circle magazine #29 9 contents ^
10. 10. PROGRAM IN PYTHON - PART 3 the same thing, except the While we should really do some but we can just +======================================+ padding goes on the left side. error checking, you can use print it. Notice | Item 1 3.00 | Now for the neat bit. Using that as something to play with that we are | Item 2 15.00 | substitutions we throw on your own. So...our Fmt sending 30 for +--------------------------------------+ together a big string and return function is really only two lines the width of the | Total 18.00 | +======================================+ that to the calling code. Here is of code outside of the left bit and 10 our next line. definition line and any for the width of comments. We can call it like the right. That equals the 40 output should look something return 'ss' % ('| this. that we sent to our like the text shown above right. ',val1.ljust(leftbit-2,' TopOrBottom routine earlier. '),part2.rjust(rightbit-2,' '),' |') print Fmt('Item So, fire up your editor and type While this is a very simple 1',30,item1,10) example, it should give you a in the code below. While this looks rather good idea of why and how to Again, we could assign the use functions. Now, let's daunting at first, let's dissect it Save the code as return value to another string, extend this out a bit and learn and see just how easy it is: 'pprint1.py' and run it. Your - We will send back our created string to the #pprint1.py calling code. #Example of semi-useful functions - We are going to stick in def TopOrBottom(character,width): 4 values in the string. Each # width is total width of returned line %s is a place holder. return '%s%s%s' % ('+',(character * (width-2)),'+') - Starts the variable list - Print these literals def Fmt(val1,leftbit,val2,rightbit): # prints two values padded with spaces - # val1 is thing to print on left, val2 is thing to print on right Take the variable val1 that we # leftbit is width of left portion, rightbit is width of right portion were passed, left justify it part2 = '%.2f' % val2 with spaces for (leftbit-2) return '%s%s%s%s' % ('| ',val1.ljust(leftbit-2,' '),part2.rjust(rightbit-2,' '),' |') # Define the prices of each item characters. We subtract 2 to item1 = 3.00 allow the '| ' on the left side. item2 = 15.00 - # Now print everything out... Right justify the formatted print TopOrBottom('=',40) print Fmt('Item 1',30,item1,10) string of the price rightbit-2 print Fmt('Item 2',30,item2,10) spaces. ' |' - finish the string. print TopOrBottom('-',40) print Fmt('Total',30,item1+item2,10) That's all there is to it. print TopOrBottom('=',40) full circle magazine #29 10 contents ^