Skip to content
Programming Decision making
All classes Beginner About 12 min Premium

Three tries for a PIN

An ATM gives you three tries to type your PIN. Choose the PINs typed on each try and run the program: a while loop keeps asking while the card is locked AND fewer than three tries are used, then either welcomes you or blocks the card.

Core concept

A loop condition joined with AND stops as soon as either part is false: here when the PIN is right or when three tries are used up

Topic: Decision making

When you use a bank card at an ATM, it asks for your secret PIN. If you type it wrong, it lets you try again, but only up to three times; after that the card is blocked, so a thief cannot keep guessing. A program does this with a while loop: Steps that repeat, either a set number of times or until something becomes true. All key words whose condition: A question with a yes or no answer that decides what the program does next. All key words has two parts joined by AND: keep going WHILE tries < 3 AND the card is still locked. AND is true only when both parts are true. So the loop stops as soon as either part becomes false: the PIN was right (the card is unlocked), or three tries have been used. After the loop, an IF decides whether to say welcome or that the card is blocked.

Key words explained

What you will learn

  • Explain how a loop condition joined with AND stops.
  • Trace a loop that counts tries and checks an answer each time.
  • Predict whether the card is unlocked or blocked for given PINs.
  • Explain why a limit on tries keeps a card safe.

Before you start

  • Know how a WHILE loop repeats and how IF / ELSE chooses.

Materials

  • A notebook to trace the tries

Equipment

  • None

Procedure

  1. Run the program: the first PIN is wrong, the second is right.
  2. Make all three PINs wrong and run it again.
  3. Make the first PIN right and run it.
  4. Answer the practice challenges.
Premium experiment Create a free account