The other day I was working on a simple piece of code that would allow me to print something like this:

First Line
 
 
 
Second Line

I know, this looks incredibly basic for those incredibly advanced programmers), but it’s where I’m at.

I wanted to create a piece of code that would allow me to indicate how many spaces I wanted in between the “First Line” and the “Second Line”.  To do so I decided to create a function.  A function, in programming lingo, is a named sequence of statements that preforms a desired operation.  In other words you create a definition for a term like a dictionary creates a definition for a word.  However for programming, this definition doesn’t just sit there on the page or in your mind, like word definitions in dictionaries will do, it does something.  Creating a function is like defining a verb.  You explain the action the word is to take.  “Beating” as in beating a egg, has a certain action prescribed to it.  Here you have the same.  

Below you’ll seem my bit of code.

def newline (x):
y = 0
while x > y:
y = y + 1
print
x = int(raw_input("Please enter in a number:"))
print "First Line"
newline(x)
print "Second Line"

 “Def” is the keyword for definition.  In the first line you can see that I am defining “newline,” which is the name of my function.  The subsequent indented statements are the parameters, or requirements, that I’ve attributed to the action the function ‘newline’ is to take.  The x in that first line of the definition, inside the parenthesis, is the variable.  

While reading this function’s definition, you can see that y must be equal to zero in the beginning.  The while statement creates a loop, so that I can continue to process information until I get the result I’m looking for.  Then, all the while x (my variable) is greater than y I will be doing two other things.  The first is adding 1 to the value of y.  The second is ‘print’ or printing.  This ‘print’ is a keyword, which is a term used in python to issue certain actions.  In that way it’s like a function.  ‘print’ will print a new line.  Sometimes you can choose to put information in this new line, or as with this function, you can choose to enter in nothing.  Therefore ‘print’ satisfies my first wish from this program.  It creates a new blank line.  Now on to the looping mechanism while.

Starting at the beginning of this definition we once again see that y is equal to zero.  So, as shown by the second and third line, as long as x is greater than y then I will be adding 1 to the value of y.  So, starting from the beginning of the function we have this:
  1. The first time I go through this loop y will be equal to zero.  
  2. Then I add 1 to zero.  
  3. The second time I go through it y will be equal to one, then I add 1 to 1.  
  4. The third time y would be equal to 2, and so on.  Anyways, you get the gist of that part.
The last part of the function says simply print, in other words print creates a new line or blank line.  In a way it acts the way pressing the ‘enter’ button does in a Word document.        

def newline (x):
y = 0
while x > y:
y = y + 1
print
x = int(raw_input("Please enter in a number:"))
print "First Line"
newline(x)
print "Second Line"

Once I’ve defined my function I have to write up my final pieces of code.  You can see the code highlighted above.  That is the code that actually runs something.  First I define “x”.  The ‘int’ means I want anything the person puts in to be an integer (or a number that can do number related things).  The second “raw_input” indicates that I want the user to be able to interact with the program, and that in doing so they will input something.  The last is my question I ask the user, “Please enter in a number.”

The last pieces of code ‘print’ simply asks for it to print the string “First Line.”  Then I run the function newline, where I ask the user to input a number.  The number given will determine the amount of empty lines inputted.  

First Line
 
 
 
Second Line

The example above uses the code I have been explaining.  Here the variable x was replaced with the number 3, therefore giving it 3 empty lines of space until “Second Line” was printed.  

Alright, that’s all from me today.  Off to some Curvy madness.