Featured

How to Create Your First Python Program

As an introduction to programming with Python, let's create a small sample program—the Guessing Numbers game.

 

The idea of the game is as follows: The player should guess a number specified in the program. For this purpose, they can make as many attempts as they like. After each attempt, the program informs them whether the guessed number was too big, too small, or exactly right. Once the player guesses the number, the program outputs the number of attempts and exits. From the player's point of view, the whole thing should look like this:

 

Guess: 42

Too small

Guess: 10000

Too big

Guess: 999

Too small

Guess: 1337

Great, it took you only 4 attempts!

 

Let's now switch from the flow protocol to the actual implementation in Python.

 

Guessing Numbers: A Simple Example

 

The highlighted areas of the program are discussed again in detail ahead.

 

Initialization

During initialization, the variables needed for the game are created. Python distinguishes among different data types, such as strings, integers, or floats. The type of a variable is determined at program runtime based on the value assigned to it, so it’s not necessary to specify a data type explicitly. A variable can change its type in the course of the program.

 

In our game, variables for the searched number (secret), the user input (attempt), and the attempt counter (counter) are created and given initial values. The fact that attempt and secret have different values at the beginning of the program ensures that the loop will actually start.

 

Loop Header

A while loop is initiated. A while loop runs as long as the condition named in the loop header (attempt != secret) is met—in this case, until the attempt and secret variables have the same value. From the user's perspective, this means that the loop runs until the user input matches the number to be guessed.

 

The loop body belonging to the loop header can be recognized by the fact that the following lines have been indented one step further. As soon as the indentation moves one step to the left again, the loop body ends.

 

Loop Body

In the first line of the loop body, a number entered by the player is read and stored in the attempt variable. The user's input is read using input("Guess: ") and converted to an integer with int. This conversion is important because user input is generally read as a string. In our case, however, we want to continue using the input as a number. The string "Guess: " is output before the input and is used to prompt the user to enter the number.

 

After reading, it’s checked individually whether the entered number attempt is larger or smaller than the searched number secret, and a corresponding message is output via print. Finally, the attempt counter counter is increased by one.

 

After the attempt counter is incremented, the loop body ends because the next line is no longer indented below the loop header.

 

Screen Output

The last program line is not part of the loop body. This means that it’s not executed until the loop is completely run—that is, until the game is won. In this case, a success message and the number of attempts required are output. The game is finished. Now create your first Python program by writing the program code to a file called game.py and running it. Change the start value of secret, and play the game.

 

Editor’s note: This post has been adapted from a section of the book Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser.

Recommendation

Python 3
Python 3

Ready to master Python? Learn to write effective code with this award-winning comprehensive guide, whether you’re a beginner or a professional programmer. Review core Python concepts, including functions, modularization, and object orientation, and walk through the available data types. Then dive into more advanced topics, such as using Django and working with GUIs. With plenty of code examples throughout, this hands-on reference guide has everything you need to become proficient in Python!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments