Skip to content
ExamSpine

Lesson 2.6 · Write it

Password check

A whole programPython or Java6 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads a password (text) and checks whether it matches the secret password open123.

  1. Always print Password: followed by the typed password on the first line.
  2. If the password matches open123 exactly (capitals, digits and all), print Access granted on the next line.
  3. If it does not match, print Access denied on the next line.
  4. The comparison is case sensitive: Open123 is not the same as open123.
  5. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use == to compare text. In Java, use .equals (never == for text).

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • the correct password (rules 1 and 2)

    You type

    1. typed: open123

    It prints

    Password: open123
    Access granted
  • an incorrect password (rule 3)

    You type

    1. typed: wrong

    It prints

    Password: wrong
    Access denied
  • wrong case: denied (rule 4)

    You type

    1. typed: Open123

    It prints

    Password: Open123
    Access denied
solution.pySolution.java