16. Enter PIN (+10 XP)


# Loop


In programming, a loop is used to repeat a block of code until a specified condition is satisfied. It's another tool that's used a ton!

People will often use the generic term “iterate” when referring to loops; iterate simply means “to repeat”.

The first kind of loop that we are going to learn is the while loop. You can think of the while loop like a traffic circle.

Each lap is one iteration! The car will iterate over and over again until it can't do so anymore.





# Instructions


Before we dive deep into the while loop, let's see a demo using a bank's ATM. 🏦

Create a enter_pin.py program and type in the following:

          
          print("BANK OF CODÉDEX")  

          pin = int(input("Enter your PIN: "))
          
          while pin != 1234:
            pin = input("Incorrect PIN. Enter your PIN again: ")
          
          if pin == 1234:
            print("PIN accepted!")
          
        

Run the program. When the program prints out the "Enter your PIN: " message, try the following:


  1. Press 1111 and then enter key.

  2. Press 2023 and then enter key.

  3. Press 1991 and then enter key.

  4. Press 1234 and then enter key.


Back

Solution: enter_pin.py