Functions

Now we have learned the basics of Python programming: variables, collections and loops. If you have followed the examples and the tutorial you might feel that sometimes we use too much code and it can be leveraged a bit. But maybe you do not know how to do it.

This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.

Continue reading

Guess the number — the first approach

Now let’s write a script which implements a basic “Guess the number” game. The rules of the game are:

  • The user selects if he/she wants to guess a number between 1 and 100 or 1 and 1000
  • Based on the number-range the user has a fixed number of guesses
  • The application generates a number to guess
  • The user enters a number
    • if it is the secret number the application congratulates the user and ask if he/she wants to play another round or not
    • else the application tells the user if the secret number is less or greater than the number provided
    • the guess count is increased
  • if the user used up all guesses then the application tells him/her the secret number and asks if the user wants to play another round or not

As you can see, the rules are simple but seem complicated to implement. It is up to you how you want to handle wrong input types (no numbers).

This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.

Continue reading

Loops

Loops allow to loop through the program code and repeat blocks of code until a given condition is fulfilled or not fulfilled. The code which is repeated is called the loop’s body.

This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.

Continue reading

Decision making with conditionals

After this vast amount of introductory articles with the basic types of variables in Python 3 it is time to go on and introduce conditional operators which help us create an application flow based on different conditions at runtime.

This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.

Continue reading