Thenjiwe kubheka
9 min readNov 20, 2020

--

Python Beginner Tutorial: Loops

Simplified Coding Tutorials

For Loops

The best way to explain for loops is to draw a star using tumi the turtle.

Notice how we had to repeat the tumi.forward () and tumi.back () multiple times.

This is not feasible in the programming world as it wastes time, we want to avoid boring, repetitive tasks.

To solve this problem we need to use a loop.

In our case a counting loop also known as a for loop.

A counting loop is a command that tells the computer to repeat certain commands over and over for a certain number of times.

We want the computer to repeat tumi.forward (250) and tumi.left(135) 8 times.

This calls for a counting loop as we want the computer to count to 8 and each time it counts, it should repeat the commands

So let’s begin the loop:

This command is telling the computer to count 8 times and while doing that repeat tumi.forward(250) and tumi.left(135).

The colon(:) after range(8) tells the computer there are more commands coming.

And those commands are the forward and left commands.

Also notice the space between the commands in line 6 and 7.

The space between the tumi.forward() and tumi.left() command.

This space is called indentation.

It is a way to tell the computer that the tumi.forward() command and the tumi.left() command belong to the for loop command.

Indentation is the type of syntax used in Python, other programming languages such as Java use curly braces {}.

So now we have our star with less effort, and minimal typing.

The computer doesn’t get bored, it does not mind repetition, thus we use a For loop to ease our lives and pass on the task of repeating to the computer.

Let’s draw a more complex shape using a for loop, we want the computer to count to 100 now and draw us a artistic star.

We get a flower like star, but the computer hasn’t finished drawing. I stopped the program execution as it was too slow.

Soo we are going to increase the speed and add color to our star.

The speed command is as follows

The levels of how fast the turtle draws is measured from 1 to 10, 1 being very slow and 10 being very fast.

Now let’s add our color command to the star.

Let’s give the full commands and draw our star.

The star looks better now, more colorful and it finished as we increased the speed.

Play around with for loops using turtle graphics and feel free to send us your designs via our social media platforms.

Now we are going to move on to the other type of loop known as the while loop, but we will not be using turtle, we will be designing a guessing game.

While Loops

Like the for loop, the while loop also repeats commands.

The difference is that the while loop works with a condition.

It is also known as a conditional loop as it repeats commands and stops when the condition is true.

Let’s create a game to best explain a while loop.

We want the computer to generate random numbers and the player must try to guess these numbers.

Every time a player guesses the computer will report back to the player, telling them if they were Too High, Too Low or Correct and Congratulations.

We are going to divide our game into 4 steps:

Step 1

The computer has to pick a random number

Step 2

The player makes a guess

Step 3

The computer compares the guess to the random number it generated

Step 4

The computer has to print out “Too High”, “Too Low” or “Correct and Congratulations”

Step 1

We need to tell the computer to pick a random number, if we want to use the random command we must import the command random.

Then our random number needs a variable, we will call it secret.

The secret number is a random number that will range between 1 and 12.

I picked the range 1 to12 as this will make it fun we don’t want the player to guess the whole day.

Now let’s tell the computer what our range is and our variable is,using this command;

This command is telling the computer our variable secret is equal to a random number between 1 and 12.

The randrange command will count 1 up to 13 but will not include the number 13.

Now let’s print out secret using the print command to finalize step 1.

Step 1 is completed, the computer is successful generating random numbers, every time we run the program it generates a random number.

Step 2

We want our player to make a guess and thus we need to use the input command, as we want the player to input a number he or she is guessing.

The command is telling the computer the variable guess is equal to whatever the user inputs/ enters into the system and we want the input to be a number and not a string so convert the value to an integer value.

Step 2 complete now to move on to step 3 and 4 which go together as we want the computer to compare the number that is guessed to the random number it generated then print out a response to the user.

So we can combine step 3 and 4

To compare guess and the generated random number we will use the decision statements.

With decision statements we are telling the computer decide what to do based on what we told you, let’s write a decision statement for the computer.

So we are telling the computer if guess is greater than secret, print Too High, so if its not greater than check if guess is lower than secret, if it is print Too Low. And if it is not greater or lower than secret it means the player got it right, so congratulate them and print Correct and Congratulations.

Let’s perform some tests to see if it works perfectly.

Great the program is working fine, we just need to remove the print secret command as we do not want the player to cheat.

Another problem is that the player only gets one chance to guess then the program ends and we must run the program again, so we do not want that, we want the player to guess until he/she gets the random number.

We also want the program to tell the player how many guesses it took until he/ she got it right, this we want to print the score.

So how do we fix our problem of the player only getting one guess?

We can use a for loop but remember the for loop is a counting loop and we do not know how many times it will take the player to get the right number.

So we need to use a while loop, remember I explained a while loop is a conditional loop.

It will repeat the commands until the condition is met, until the player guesses the number right.

So our while loop

So we are telling the computer, while guess does not equal secret, continue to loop.

This != represents does not, but we will discuss it and others in another lesson.

Before we run our while loop, we need to give guess a starting value so that the computer can compare guess with something.

When assigning a value for guess we cannot assign a value that is within our secret range, so we cannot pic a number between 1 and 12.

Let’s say we pic 2 as the value of guess and the computer also pics the number 2 as the random number then the loop won’t start, the game will end before it even starts.

So we will pic the number 0.

Let’s test to see if the while loop is working and if the player gets unlimited guesses.

So from our final command we are going to remove the print secret command.

Then we want to keep track of how many times the player guessed so the computer can print out a score at the end.

We also need a variable for this and we are going to call it number_of_guesses and assign an initial value, so the number of guesses should start at 0, as the player has not made any guesses yet.

We also want to add 1 to number_of_guesses every time a player makes a guess.

Lastly we want to print the score;

Let’s assign a variable to keep track of the number of guesses.

We also want number_of_guesses to increment by 1 every time a player makes a guess.

Then lastly our print function to display the score;

Let’s run the full program:

Summary

1. In this article we discussed two types of loops; For and While loop

2. For loop is known as a counting loop, used when we know how many times we want the loop to repeat the command

3. While loop is known as a conditional loop, used to repeat commands until a condition is true or has been met

4. Both the For and While loop are used to eliminate the task of repetitive typing, they make our lives easier as programmers

--

--

Thenjiwe kubheka

Software Engineer from South Africa, who loves mathematics. Fascinated by machines, plays around with a lot of data, constantly researching AI.