Content

Installing python

The first step to start writing python is having a computer. Then install in this computer the python development environment (basically programs that help us write python): https://www.python.org/ (obviously only install from the official website). NOTE: During the installation select the option “add to path” during the first screen (and unless you really know what you are doing) select normal install.

Executing python

Open the newly installed IDLE (Python Integrated Development and Learning Enviroment), type mathematical equations and execute them by pressing ENTER, exs:

What you did was telling python to execute commands and it showed you their results, any mathematical equation in python is automatically resolved and returns the result, so when you execute one in the REPL (the >>> part of IDLE) it shows you the result. But let’s create a file to better write programs

In IDLE click File/New File (or ctrl + N). This will create a new file for you to script python on, first save it by clicking File/Save (or ctrl + S), save the file anywhere.

What is IO?

The first thing you need to understand about python (and any programming language) is IO, the famous Input and Output. All programs, no matter of what type (web apps, mobile apps, AI, games, etc. ) are in the end composed of Input and Output. All programs work the same way:

Now, there are varios types of input and input to be used in python programs, you can have a graphical user interface (the famous GUI) and check if the user interacted with something on it, you can create a web server which interacts with Network requests (like someone trying to access your server) or the classic, inputing and outputing text in the terminal of a computer.

Output in python

Inside your python program, type print(1) and then execute it by pressing Run/Run module (or F5). It will create a window with the result of your program. You can pass any kind of mathematical equations to it and the result will be printed to your terminal, exs:

Of course, text is something which we can also use, but to use text in python you have to “embrace it with double quotes”:

The print command can also concatenate values, for example:

print(" the result of 1 + 1 is:", 1 + 1)

Basically this allows you to show the result of operations to a user, it adds space between it and the last piece of text, so mind that. And of course you can use variables with it:

result = 100 / 5
print(" the result of 100 / 5 is:", result)

Input in python

Of course, with output you can already do a lot, but understanding what a user wants is very important, so let’s ask a few things to them. To do it just type: input("Your question goes here: "). What happens when this command is read? The terminal of the computer freezes up and waits a user to type something and send it to the computer (by typing the ENTER key). And as anyone would guess, it is useless, because it doesn’t save what the user types, to save it you have to save the input to a variable, lets see a example:

a = input("Type something: ")
print("You typed: ", a)

With this you can already do a few things, for example saying a good day to the person in particular:

name = input("What is your name: ")
print("Good morning", name, "how are you?")

Coding python in style

And now with that in mind, let’s start taking things up a notch. First let’s get off IDLE, yes it is cool, and it is written entirely with python (which is awesome), but you can write python with any kind of text editor you want, so let’s get to know a more professional tool to write python with. We will use the most popular code editor in the world, visual studio code: https://code.visualstudio.com. Download it and install it in your computer.

Now open VS Code and create a new file somewhere with the name you want plus the extension .py. Any file saved with the .py extension can be executed by python, that’s exactly the extension IDLE used to save our last scripts, so with that in mind, you can write python with any text editor you want (for dignity sake, don’t use word).

But a doubt appears now, how do we execute the program? Well, it’s complicated. You see, python is nothing more than an executable that your computer runs, it is literally a .exe file in your computer, and when you point this .exe file to a .py file it reads it’s contents and executes it, so let’s do this to the file we created in VS Code. To do this open a cmd in Windows in the folder where you saved your .py file. Then with the cmd open type python yourFileName.py, and python will execute it.

TIP: With VS Code, you can not only open a file, you can also open a folder. When you open a folder with VS Code, you can then execute View/Terminal, which will open a terminal in the folder and than it is easier to execute your program.

Creating a text adventure game

Now then, to the challenge. Let’s do a complex program with what we learned until now. Let’s do a text adventure! First, an example, access the website http://play2.textadventures.co.uk/Play.aspx?id=jmlw67s-skiys3u4-0gaqq and play it a bit.

Well do one of our own, but simpler, without the images and such, just text.

Here’s what well do:

print("One upon a time, there was a person named Roberto, who was eating a ice cream. When all of a sudden a meteor hit the place he was, instantly killing him. Now he is in heaven, so what does he do now?")
print("1. He looks for famous people")
print("2. He decides to do absolutely nothing")
print("3. He sits down and cry")

answer = input("What should Roberto do? ")

if answer == "1":
    print("Congratulations, you encontered Freddie Mercury, you hear him sing satisfied")
if answer == "2":
    print("Congratulations, you do nothing, as there are no consequences here you enjoy the present. CARPE DIEM")
if answer == "3":
    print("...")

Of course there are ways to improve the program:

print("")
print("                  The adventures of Roberto                    ")
print("")
print("One upon a time, there was a person named Roberto, who was eating a ice cream. When all of a sudden a meteor hit the place he was, instantly killing him. Now he is in heaven, so what does he do now?")
print("1. He looks for famous people")
print("2. He decides to do absolutely nothing")
print("3. He sits down and cry")

answer = "0"
while answer == "0":
    answer = input("What should Roberto do? ")

    if answer == "1":
        print("Congratulations, you encontered Freddie Mercury, you hear him sing satisfied")
    elif answer == "2":
        print("Congratulations, you do nothing, as there are no consequences here you enjoy the present. CARPE DIEM")
    elif answer == "3":
        print("...")
    else:
        print("Invalid answer, try again")
        answer = "0"

print("The END...")