Loops

Requirements:

  • Linux Distribution
  • python3
  • A Shell Interface

My Setup:

  • Debian GNU/Linux
  • python3
  • BASH

Sometimes you need to repeat a couple functions or statements multiple times.
For this, you would use a looping mechanism. There are two mechanisms that python
provides:

  1. for – loop used when the amount of times you need to repeat a block of code is known beforehand.
  2. while – loop used when the amount of times you need to repeat a block of code is not known, but computed after the statements are complete

For loop example :

for VARIABLE in SEQUENCE:
  #do stuff here

The sequence could be a container (like a list), or the range function. The range function allows for you to iterate to a user defined point. For example:

for i in range(10): #for each number from 0-9
  print("i is :", i)

Within the interpretter, this should print:

clim@debian:~/Desktop/tests/pytests/loops$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(10):
...     print("i is: ",i)
...
i is:  0
i is:  1
i is:  2
i is:  3
i is:  4
i is:  5
i is:  6
i is:  7
i is:  8
i is:  9
>>> quit()
clim@debian:~/Desktop/tests/pytests/loops$

You can also declare a starting point as such:

for i in range(5,15): #for each number from 5-14
  print("i is :", i)
clim@debian:~/Desktop/tests/pytests/loops$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(10):
...     print("i is: ",i)
...
i is:  5
i is:  6
i is:  7
i is:  8
i is:  9
i is:  10
i is:  11
i is:  12
i is:  13
i is:  14
>>> quit()
clim@debian:~/Desktop/tests/pytests/loops$

While loops on the other hand, are used when you dont know how many times you will need to execute the said block of code. This could be used, for example , to keep a program running (until something happens which sets a running variable to False).

while CONDITION:
  #do something here

Lets write a guessing game! It will use a couple examples we have yet gone over, but I will explain them here.

import random                      # Import the random module

number = random.randrange(1, 100) # Get random number between (1-100)
guesses = 0
guess = int(input("Guess a number between 1 and 100: "))

while guess != number:
    guesses += 1
    if guess > number:
        print(guess, "is too high.")
    elif guess < number:
        print(guess, " is too low.")
    guess = int(input("Guess again: "))

print("\n\nCorrect! It took ", guesses,  "guesses!")

What I did was import a function from a module called random. I then set a number to a random number within the range of 1 and 100 . I then used a function called input to get the keyboard input of the user and then convert it into an integer using the int() function. That value is to be assigned to a guess variable which will check whether or not, the number is equalled to the one we generated with the random function.

clim@debian:~/Desktop/tests/pytests/loops$ python3 guess.py
Guess a number between 1 and 100: 44
44  is too low.
Guess again: 68
68  is too low.
Guess again: 86
86 is too high.
Guess again: 75


Correct! It took  3 guesses!
clim@debian:~/Desktop/tests/pytests/loops$

One thought on “Loops

Leave a Reply