Let’s get rolling with our Python tutorials by starting off with some basic assumptions:
1) You have already installed Python on your machine.
2) You have successfully used the Integrated Development Environment (IDE) that either came with Python or that you installed separately.
3) You know how to save your Python files in some convenient place that you can access later for further editing / improvement.
Of course there are several other assumptions that we could mention, like the idea that you have a laptop, access to the internet, an actual desire to code, etc., but those first three seem like enough danger to start with (and if anyone is forcing you to code against your will, seek medical attention immediately). If one or all of the first three are not true, then go about getting yourself situated with Python before digging in here.
I’m going to skip the parts about getting Python installed on your computer, deciding which IDE to use, and all the rest. For now, we’re just going to jump in at the point where we will actually be in danger of drowning: the point where we start…to code.
In this tutorial, we’re going to code the most simplistic and monotonous guessing game perhaps ever played by anyone. I’m not staking claims to this code as my original creation. It most certainly is not. You can find variations of this game all over the web since it does a fantastic job of introducing you to some very basic Python language concepts. We will also not claim to be proud of this game’s entertainment value, and will only use it to illustrate how to get started on your way to developing better programs that do more useful stuff.
Warning! I am a beginning programmer, perhaps even more infantile in my development path than you are. Mistakes will be made, and THAT’S OKAY. Mistakes are how we learn. If you’re willing to take a turn on the coding dance floor with me, let’s just agree that we’ll have a lot of fun at the same time that we may miss a beat every now and then. If others awkwardly stare, turn a blind eye and keep coding.
As I type within my block of text, I will try to offer comments for many of the things that I discuss within this post so that you will have a pretty good idea of what I did even after you leave this blog and perhaps use this same code for your own purposes. I’m a technical writer by trade, so I’m all about helping you understand important concepts as we come to them. However, because we are learning together, all will not necessarily be revealed all the time.
Let’s get started…
First off, a couple of definitions, since they will help you to understand this content better.
Comment – #Anything that you type in Python as a comment should be preceded by a hashtag (#). This means that whatever you type after the # is useful to you and others as a reminder of what’s going on with the code. Otherwise, it is ignored by Python and does not affect the code in any way.
Variable – Oh no, sounds like Algebra! Don’t worry. Let’s restrict this definition to something very simple: a placeholder, something of your own choosing to store a value in the computer’s memory for recall later on.
HERE’S WHERE WE START CODING…
#Guessing Game (a title for our own purposes)
#Let’s create a variable called “secret_number” to represent the number to be guessed. Let’s set its value at “10.”
secret_number = 10
#Now let’s create a second variable called “player_guess” to represent the guess the player offers each time. We’ll set it to ‘0.’
player_guess = 0
#Next, we will set up a “while loop” so that, as long as the player’s guess does not equal the secret number, the indented code beneath it will run, that is until the player DOES guess the secret number. At that point, the loop ends, and so does the game.
while player_guess != secret_number:
print(“What is the secret number?”)
player_guess = int(input())
if player_guess == secret_number:
HERE’S WHERE WE STOP CODING
In your IDE, you would type in the above code (probably without the comments, although you can certainly introduce your own to remind yourself of what’s going on with the code). Then you press a Run button or something similar to get your code to run.
If you are a beginner like me, it is VERY likely that there will be an error or two that will cause your program not to run as intended. That’s expected and absolutely okay. It doesn’t mean that you weren’t meant to code or that it’s time to seriously start thinking about a Plan B. No, it probably just means you left out a colon or something. Relax! Go back through everything and check it twice, thrice, even four times is nice.
Once you get your Guessing Game to run correctly, type in a few incorrect guesses, followed by the correct answer (in our case, it’s ’10’ but you can change the value, then play the game again as many times as you like).
Here is the sample code as it looks inside my IDE (note that the indents are important. If you removed them, you should put them back in. No, seriously put them back now!)
If this is the first time you actually created a program and it ran successfully…
I want to offer you a sincere CONGRATULATIONS! Seriously. This is an amazing feeling, right? That’s the magic of Python, and there’s a lot more where that came from.
Stay tuned. Python Tutorial #2 is coming soon. We’ll go through this same code again, but with a fine tooth comb to explain some of the elements we mostly passed over this time around. And we’ll add some cool new elements to make our game twice as exciting as it is right now. See you there!