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.
- Always print
Password:followed by the typed password on the first line. - If the password matches
open123exactly (capitals, digits and all), printAccess grantedon the next line. - If it does not match, print
Access deniedon the next line. - The comparison is case sensitive:
Open123is not the same asopen123. - 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
- typed: open123
It prints
Password: open123 Access granted
an incorrect password (rule 3)
You type
- typed: wrong
It prints
Password: wrong Access denied
wrong case: denied (rule 4)
You type
- typed: Open123
It prints
Password: Open123 Access denied