IntroToPythonForTotalBeginners

Intro

  • Learning to program might take a few tries
  • Focused on introducing a few key programming concepts
  • Show what a program is, how it works
  • When I was a boy ...

What is a program?

  • a series of instructions for the computer to follow

BUT...

  • computers are not smart, only obedient
  • they can't infer what you mean, so small errors in writing a program will make the program stop
  • the will also follow any directions, no matter how stupid

Resources

Here are some links if you want to follow up:

The Game

So in this session, I will get you started building a classic adventure game. It will be totally on the command line, because it's a bit easier that way. Once you have the basics down, you can start working on a program with a graphical interface.

Before writing a program, it's a good idea to sketch out what you are trying to make. Here is sketch of what the game will be:
map.png

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"

Part 5 - more on variables

Let's handle looking at the lamp and getting the bulb in the living room. First, we want the player to see that there is a bulb that they can take. But if they already took the bulb, we don't want them to keep seeing the bulb right? So we handle this by creating a global variable. "global" just means that you can use the variable everywhere in the program, not just within the function where it was defined. Create a global variable at the top for your code file like this:

   1 global has_bulb
   2 has_bulb = False

So we created a global variable called "has_bulb" in line 1. If you don't want the variable to be available everywhere in your program, just leave out line 1. In line 2, we assigned the variable to be "false". As in, the player does not yet have the bulb, right? If you assign a variable to be True or False, this is called a boolean, btw. You've already seen that variables can be strings. They can be numbers as well.

So no let's use the variable in the enter_living_room() function.

   1 def enter_living_room():
   2     global has_bulb
   3     print "You are in the living room. There is a tv cabinet, a"
   4     print "table with some dishes, and a lamp."
   5     command = raw_input("Enter a command:")
   6     if command == "look lamp":
   7         print "It is a floor lamp. It is not plugged in."
   8         if not has_bulb:
   9             print "There bulb looks new"
  10     if command == "take bulb":
  11         print "You now have a bulb"
  12         has_bulb = True

This adds a couple of things. Line 2 tells the function that it should use the global variable has_bulb. Lines 8 and 9 make it so that the line about the bulb is only printed out if the player does not yet have the bulb. Line 12 sets the has_bulb variable to True. Next time the player looks at the lamp, the line about the bulb won't be printed.

Part 6 - One Last But

But this will never work right, because the program exits after the player takes the bulb. They'll never be able to look again, right? Right. So we need to run through the enter_living_room() function again. This is easy because a function can call itself!

So here's the whole program so far:

   1 global has_bulb
   2 has_bulb = False
   3 
   4 def enter_living_room():
   5     global has_bulb
   6     print "You are in the living room. There is a tv cabinet, a"
   7     print "table with some dishes, and a lamp."
   8     command = raw_input("Enter a command:")
   9     if command == "look lamp":
  10         print "It is a floor lamp. It is not plugged in."
  11         if not has_bulb:
  12             print "There bulb looks new"
  13         enter_living_room()
  14     if command == "take bulb":
  15         print "You now have a bulb"
  16         has_bulb = True
  17         enter_living_room()
  18 
  19 print "You are outside the front door of the house. The prize is inside."
  20 print "You can enter commands to do stuff"
  21 print "The commands are go, look, take, use, and put"
  22 print "Use commands with objects that you see around"
  23 print "For example, you can enter 'look door'"
  24 
  25 command = raw_input("Enter a command:")
  26 if command == "look door":
  27     print "The door to the house. It looks unlocked"
  28 elif command == "go door":
  29     print "You go through the door into the living room"
  30     enter_living_room()
  31 else:
  32     print "You can't do that here"

UbuntuOpportunisticDeveloperWeek/IntroToPythonForTotalBeginners (last edited 2010-02-25 04:19:29 by rick-rickspencer3)