IntroToPythonForTotalBeginners

Differences between revisions 3 and 5 (spanning 2 versions)
Revision 3 as of 2010-02-24 22:53:59
Size: 3069
Editor: 97-126-115-182
Comment:
Revision 5 as of 2010-02-24 23:18:37
Size: 7468
Editor: 97-126-115-182
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from UbuntuOpportunisticDeveloperWeek/IntroToPythonFOrTotalBeginners
Line 9: Line 10:
 1. A text editor (usually gedit) open. To get started, save an empty text file and name it "prize_hint.py"  1. A text editor (usually gedit) open. To get started, save an empty text file and name it "prize_hunt.py"
Line 55: Line 56:
Instead of just quiting, we want to write some code to see what command the user entered, and do something depending on what they put in. Instead of just quiting, we want to write some code to see what command the user entered, and do something depending on what they put in. Let's make it so they can look at the door or go through the door, any other command we will ignore.

{{{
#!python
if command == "look door":
    print "The door to the house. It looks unlocked"
elif command == "go door":
    print "You go through the door into the living room"
else:
    print "You can't do that here"
}}}

Let's look at what this block of code does, as conditional code like this is very very common part of programming. Also, there is some more python syntax here that you need to learn. Starting with line #1, what this code says is "if command has been set to the string 'look door' then run the lines of codes that are indented below here". Typically, programmers will read this as "if command equals 'go door'". The two "=" are important. There are two because sometimes you write code that sets the value of a variable, and that uses only one "=".

Note that line number 2 is indented exactly 4 spaces. Python uses indentation to define what blocks of code go together. So since line 2 is indented, if the users types something other than "look door" it will skip over line 2. Early on you may write lots of bugs because you get indentation wrong.

Line 3 says "elif" to start. elif is read "else if". The "else" is important, because if the user did type in "look door", you just want to run the code for that condition. So if the line starts with "else" the code will be skipped if a condition before was met. If the user did not put "look door", then line 3 asks, ok, was it "go door"? Otherwise, it tries line 5. Since line 5 has no "if" it will always run if either of the other conditions were not met.

Try out some code. Questions?

= Part 5 - your own functions =
So you may notice that whatever happens, you code ends after testing the conditions. Really what you want is your code to branch off and do stuff depending on what happened. Like you probably want some code to handle the situation of the user going into the living room. They key thing to think about is that you want your code to "do something". Sounds like it could use a function. So you can make your own function for entering the living room.

Put this code at the *top* of the file:
{{{
#!python
def enter_living_room():
    print "You are in the living room. There is a tv cabinet, a"
    print "table with some dishes, and a lamp."
    command = raw_input("Enter a command:")
}}}

You have just created a function that you can call in the same way you call raw_input. "def" means define, as in define a function. Then there is the name for your function. The empty parens mean that your function can just work, it doesn't need any information.

Note that lines 2-4 are indented 4 spaces. The fact that are indented under line 1, and all indented to the same level is how python knows they are part of the same function. Let's just add a call to the function in our code for handling the command from the front door.

{{{
#!python
if command == "look door":
    print "The door to the house. It looks unlocked"
elif command == "go door":
    print "You go through the door into the living room"
    enter_living_room()
else:
    print "You can't do that here"
}}}

Notice that line 5 is where you call the function, and that python knows to run it if the command is "go door" because it's indented.

The reason that you added the function to the top of the code file is to make sure that the function got defined before you tried to use it. Here's the whole file so you can see the context:
{{{
#!python
def enter_living_room():
    print "You are in the living room. There is a tv cabinet, a"
    print "table with some dishes, and a lamp."
    command = raw_input("Enter a command:")

print "You are outside the front door of the house. The prize is inside."
print "You can enter commands to do stuff"
print "The commands are go, look, take, use, and put"
print "Use commands with objects that you see around"
print "For example, you can enter 'look door'"

command = raw_input("Enter a command:")
if command == "look door":
    print "The door to the house. It looks unlocked"
elif command == "go door":
    print "You go through the door into the living room"
    enter_living_room()
else:
    print "You can't do that here"
}}}

Intro

Resources

The Game

Part 1 - Get Ready to Code

You will need the following things:

  1. A text editor (usually gedit) open. To get started, save an empty text file and name it "prize_hunt.py"
  2. A terminal window open and in the same directory as the the prize_hunt.py file.

You are now ready to start writing a program.

Part 2 - Make the Computer Say things

Let's start by making the computer say something to the user. We do this using the "print" command.

   1 print "You are outside the front door of the house. The prize is inside."

Type this line into your code file. The "print" command at the beginning tells the computer to print the string that comes next. A string is a list of characters enclosed by either an " or an '.

Now you need to run your program. Do this by saving your file (very important to save it), and then go back to your terminal. Inside your terminal type:

python prize_hunt.py

The result should be that the string you specified is printed out for the user to see.

Let's print out some instructions for the user now. The user will interact with your program by entering some commands. Let's tell them what commands they can use.

   1 print "You are outside the front door of the house. The prize is inside."
   2 print "You can enter commands to do stuff"
   3 print "The commands are go, look, take, use, and put"
   4 print "Use commands with objects that you see around"
   5 print "For example, you can enter 'look door'"

Run this again using "python prize_hunt.py" to make sure it works.

Part 3 - Let Users Tell You Things

It's time to tell the user to give the program a command, and to use the command in the program. Add this code to the bottom of your code file:

   1 command = raw_input("Enter a command:")

This line introduces two really really common and important things in programming:

  1. The word "command" is a variable. A variable is a place where you store information in your program that want to use in other parts of your program. In this case, you don't know what the user is going to type, so it can "vary". It's "variable" information. Make sense?
  2. "raw_input" is a function. You "call" a function in a program. A function will do something, and it can also provide back some information. raw_input prints out a message that you tell it to print out and then collects whatever the user types. It then "returns" what the user typed. This line of code is storing what the function returns for you. Make sense?
  3. Some functions need some information before they can work. raw_input needs a string to display. Information that you provide to a function is called an "argument". It is usually goes inside the parenthesis.

If you run the program now, you can see that it prints your message, and let's the user type something.

Part 4 - Checking things

Instead of just quiting, we want to write some code to see what command the user entered, and do something depending on what they put in. Let's make it so they can look at the door or go through the door, any other command we will ignore.

   1 if command == "look door":
   2     print "The door to the house. It looks unlocked"
   3 elif command == "go door":
   4     print "You go through the door into the living room"
   5 else:
   6     print "You can't do that here"

Let's look at what this block of code does, as conditional code like this is very very common part of programming. Also, there is some more python syntax here that you need to learn. Starting with line #1, what this code says is "if command has been set to the string 'look door' then run the lines of codes that are indented below here". Typically, programmers will read this as "if command equals 'go door'". The two "=" are important. There are two because sometimes you write code that sets the value of a variable, and that uses only one "=".

Note that line number 2 is indented exactly 4 spaces. Python uses indentation to define what blocks of code go together. So since line 2 is indented, if the users types something other than "look door" it will skip over line 2. Early on you may write lots of bugs because you get indentation wrong.

Line 3 says "elif" to start. elif is read "else if". The "else" is important, because if the user did type in "look door", you just want to run the code for that condition. So if the line starts with "else" the code will be skipped if a condition before was met. If the user did not put "look door", then line 3 asks, ok, was it "go door"? Otherwise, it tries line 5. Since line 5 has no "if" it will always run if either of the other conditions were not met.

Try out some code. Questions?

Part 5 - your own functions

So you may notice that whatever happens, you code ends after testing the conditions. Really what you want is your code to branch off and do stuff depending on what happened. Like you probably want some code to handle the situation of the user going into the living room. They key thing to think about is that you want your code to "do something". Sounds like it could use a function. So you can make your own function for entering the living room.

Put this code at the *top* of the file:

   1 def enter_living_room():
   2     print "You are in the living room. There is a tv cabinet, a"
   3     print "table with some dishes, and a lamp."
   4     command = raw_input("Enter a command:")

You have just created a function that you can call in the same way you call raw_input. "def" means define, as in define a function. Then there is the name for your function. The empty parens mean that your function can just work, it doesn't need any information.

Note that lines 2-4 are indented 4 spaces. The fact that are indented under line 1, and all indented to the same level is how python knows they are part of the same function. Let's just add a call to the function in our code for handling the command from the front door.

   1 if command == "look door":
   2     print "The door to the house. It looks unlocked"
   3 elif command == "go door":
   4     print "You go through the door into the living room"
   5     enter_living_room()
   6 else:
   7     print "You can't do that here"

Notice that line 5 is where you call the function, and that python knows to run it if the command is "go door" because it's indented.

The reason that you added the function to the top of the code file is to make sure that the function got defined before you tried to use it. Here's the whole file so you can see the context:

   1 def enter_living_room():
   2     print "You are in the living room. There is a tv cabinet, a"
   3     print "table with some dishes, and a lamp."
   4     command = raw_input("Enter a command:")
   5 
   6 print "You are outside the front door of the house. The prize is inside."
   7 print "You can enter commands to do stuff"
   8 print "The commands are go, look, take, use, and put"
   9 print "Use commands with objects that you see around"
  10 print "For example, you can enter 'look door'"
  11 
  12 command = raw_input("Enter a command:")
  13 if command == "look door":
  14     print "The door to the house. It looks unlocked"
  15 elif command == "go door":
  16     print "You go through the door into the living room"
  17     enter_living_room()
  18 else:
  19     print "You can't do that here"

UbuntuOpportunisticDeveloperWeek/IntroToPythonForTotalBeginners (last edited 2010-02-25 04:19:29 by 97-126-115-182)