Examples Of For Loop In Python

With for loop, you will be able to iterate over a sequence of elements repeatedly until that sequence is over and there are no other elements.

In this article, we provide you with examples of using for loop in Python. You have to revise these tutorials to be able to understand the examples below.

Before we begin, try to learn these Python basic concepts:

  • To learn for loop in Python, visit our article here.

Example

This is a simple example using for loop concept. It is like a game where the user will start the game and the program will choose only one number and then when the program will iterate over the list of the numbers, there will be phases and the user will see the phase that contains the random number. If the user wins in the first phases of the game, it will be better than the last phases.

Code

#____ProgrammerCodes.com____
import random
while True:
    
    input("Press X To Begin:")
    numbers = [33,76,34,98,9,3,15,47,84]
    rand_number = random.choice(numbers)
    phase = 1
    for number in numbers:    
        if (number == rand_number):
            print("You Win In The Phase", phase)
            print("The Random Number Is:", number)
            break
        else:
            print("No It Is Not The Number!")
            phase = phase + 1
        print("___________________________________")
    print("_________________________________________________________")
#____ProgrammerCodes.com____

Code’s explanation

#____ProgrammerCodes.com____
import random
while True:
#____ProgrammerCodes.com____

In this part of the code, We imported the random module that will help us to generate and choose a random number.

We used while True to keep repeating the game after the user has finished it.

#____ProgrammerCodes.com____
    input("Press X To Begin:")
    numbers = [33,76,34,98,9,3,15,47,84]
    rand_number = random.choice(numbers)
    phase = 1
#____ProgrammerCodes.com____

We created a list that contains numbers because we will need it to let the program choose randomly a number using that choice method.

We assigned a variable called phase and set it to 1 because we will begin from phase 1 so we will need it in the for loop to know the phase when the user wins.

#____ProgrammerCodes.com____ 
   for number in numbers:    
        if (number == rand_number):
            print("You Win In The Phase", phase)
            print("The Random Number Is:", number)
            break
        else:
            print("No It Is Not The Number!")
            phase = phase + 1
#____ProgrammerCodes.com____

The for loop in this example is used to iterate over a list of numbers and in every phase of this loop, the program will check if the number of this phase is equal to the number chosen by the choice method.

As you know in every phase of the loop a there is a number from that list. The program will check it so the user will win if the program finds the number then the break statement will stop the loop. When the program does not find the choice number It will go to the next phase and do the same thing.

Result

Press X To Begin:x
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
No It Is Not The Number!
___________________________________
You Win In The Phase 8
The Random Number Is: 47
_________________________________________________________

Leave a Comment

Your email address will not be published. Required fields are marked *