Python Programming Introduction

Reading Tutorial

Python Programming Introduction

Intro

For lack of a Computer Science subject, I have labeled this tutorial under “Reading”. Programming is an important skill and is used for robotics, gaming, and many other applications. This tutorial can help you get started with Python by showing you a simple command line game. Python generally reads like plain English, so it will be easier than other languages as a start.

Sample Problem

Create a choose-your-own-adventure game with Python

Solution

This tutorial is written for absolute beginners to programming.

  1. Preface

    In order to write our code, you will need a text editor, preferably an editor built for writing code like Notepad++. These text editors highlight distinct parts of your code (key words, for example), have line numbers for easier reference, and can be customized to your preference. This tutorial also assumes you have at least Python 2.7 installed on your computer so that you can run your code (see the official page if you need to download Python).


  2. Start with Comments

    Open up a new file in your text editor and save this blank file as “ForkInTheRoad.py”. Remember where you save it! All of our code will go here. Now type:

    
    #Fork In The Road game
    
    

    Any line that has a # sign in the front is a called a comment. Comments are mostly your notes and do not affect the program at all


  3. Introduce the game

    Type:

    
    print("---------- Fork In The Road: The Game ----------")
    print("You have arrived at a fork in the road. You can go left, right, or slight right") 
    print("Which way do you want to go?")
    
    

    “print” is what is known as a built-in function that lets you display whatever is inside the parentheses. We have to put parentheses around the words that we are printing. Here, we print 3 things: the name of the game, description of settings, and the next instruction.


  4. Input

    Now we will add the next line:

    
    answer = raw_input("Type L, R, or SR: ")
    
    

    “answer” is called a variable, which lets us save information for later use. We are also using another built-in function called raw_input. This prints out something like the print built-in function, but raw_input will also save player input to a variable. Whatever the player types in next will be saved into the answer variable.

    We tell the player to enter 3 options to save a little typing (L, R, or SR without quotations).


  5. Choices

    We will use what is called an “if-else statement”, which will drive the game in different directions depending on what the player said. In other words, it is a way for us to check what was saved in the answer variable. What if the player said “L”?

    
    if(answer=="L"):
         print("There is a dollar bill on this road.")
    
    

    We type “if” to start off our if else statement, then have parentheses around our condition. We type “answer” because that is the variable we are checking. The next symbol “==” is called a boolean operator, but think of it as saying “does answer contain this next thing that we are checking?” And this next thing would be the capital letter L. We have to put quotations around L, otherwise the program will think L is another variable. Then we tell the program what to do if the player did type L, which is display what happens next.

    We will now use the “elif” key word to do the exact same thing, except specify what happens when the player enters something else, like if they want to go right and typed “R”:

    
    elif(answer=="R"):
        print("There is a lion blocking the way.")
    
    

    …or if they wanted to go slight right and typed “SR”:

    
    elif(answer=="SR"):
        print("There is a plastic fork on the road.")
    
    

    We end this part of the code with the default answer and “else”, which occurs when the user typed anything besides L, R or SR:

    
    else:
        print("You decide to stand still.n")
    
    

  6. Finishing up

    We will wrap everything up by giving the user a chance to confirm their answer:

    
    print("Are you sure you want to go this way?")
    final_answer = raw_input("Enter Y or Nn")
    
    

    We are now using a different variable to store their answer to the new question, and then we will use another if-else statement to figure out how to end the game:

    
    if(final_answer=="Y"):
        if(answer=="L"):
            print("You got the dollar.n")
        elif(answer=="R"):
            print("The lion is surprisingly friendly and lets you walk down the road.n")
        elif(answer=="SR"):
            print("The plastic fork is actually a mirage and vanishes into thin air.n")
    else:
        print("You decide to call a taxi and go home.n")
    
    

    Notice the “if” under the other “if”. The “if” that is indented under the first “if”. The indented “if” is said to be “nested”. Then we will add some more choices for the player. What this block of code says is: “Did the player enter Y for the last question? Ok, so if they said yes to confirm their choice and “Y” was saved in the final answer variable, What was saved in the answer variable/which way did they want to go in the first place? If they said L, R, or SR, then there is a custom message for each choice. However, if anything else was saved in the answer variable or if they didn’t enter “Y” to confirm their answer, there is a default message displayed.

    It is also important to note that if the player answered “N” or anything else to confirm their answer, the game will go straight to the default message, because of how the if statement is nested.

    We will add one more line to pause our program at the end:

    
    input("Press any key to exit")
    
    

  7. Run the code

    Go to the folder that has your file and double click the file. A command line terminal should open and run the game automatically. Test it out to make sure all of the options work. You just made your first Python game! Add on to the project by making more options for the player to follow, or learn more about Python at the official site.

    Here is the final code with some print functions added to help space out the lines:

    
    #Fork In The Road Game
    print("---------- Fork In The Road: The Game ----------")
    print("You have arrived at a fork in the road. You can go left, right, or slight right") 
    print("Which way do you want to go?")
    answer = raw_input("Type L, R, or SRn")
    
    print("-------------------------------")
    
    if(answer=="L"):
        print("There is a dollar bill on this road.")
    elif(answer=="R"):
        print("There is a lion blocking the way.")
    elif(answer=="SR"):
        print("There is a plastic fork on the road.")
    else:
        print("You decide to stand still.n")
    
    print("-------------------------------")
    	
    print("Are you sure you want to go this way?")
    final_answer = raw_input("Enter Y or Nn")
    
    print("-------------------------------")
    
    if(final_answer=="Y"):
        if(answer=="L"):
            print("You got the dollar.n")
        elif(answer=="R"):
            print("The lion is surprisingly friendly and lets you walk down the road.n")
        elif(answer=="SR"):
            print("The plastic fork is actually a mirage and vanishes into thin air.n")
    else:
        print("You decide to call a taxi and go home.n")
    
    input("Press any key to exit")
    
    


About The Author

Math, Computer Science, And Writing Tutor
Hello! My name is Jesson and my tutoring experience extends over 5 years, with several subjects at nearly all levels of education. I have over 3 years experience as mentor for elementary school students (K-5) in after school programs in Oakland and San Francisco, tutoring primarily with English writ...
Send Email
11 Subjects
KnowRo Tutor
5 Tutorials
$50
Vallejo, CA
Learn

Suggested Tutors for Reading Help

Ad

Varsity Tutors

(855) 475-5132 - Award-Winning Academic & Test Prep Tutors

Ad

Air Tutors

One-on-One Ivy Tutoring Partners 24/7

Ad

Link Educational Institute

Link Educational Institute

Luke N

Bethesda, MD

SAT Instructor And Humanities, Biology

Leave a Comment

Your email address will not be published. Required fields are marked *

^