IntroToPythonForTotalBeginners
|
Size: 3069
Comment:
|
Size: 3159
Comment: page was renamed from UbuntuOpportunisticDeveloperWeek/IntroToPythonFOrTotalBeginners
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| ## page was renamed from UbuntuOpportunisticDeveloperWeek/IntroToPythonFOrTotalBeginners |
Intro
Resources
The Game
Part 1 - Get Ready to Code
You will need the following things:
- A text editor (usually gedit) open. To get started, save an empty text file and name it "prize_hint.py"
- 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.
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:
- 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?
- "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?
- 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.
UbuntuOpportunisticDeveloperWeek/IntroToPythonForTotalBeginners (last edited 2010-02-25 04:19:29 by 97-126-115-182)