Lesson 2.8 · Write it
Login check
A whole programPython or Java7 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads a username on the first line and a password on the second line, and prints a result.
- Always print
User:followed by the username on the first line. - If the username is
adminand the password issecret, printWelcome, admin. - If the username is
adminbut the password is wrong, printWrong password. - If the username is anything else, print
Unknown user. - Print exactly one of those three result lines. Spaces at the end of a line are ignored.
Your program is run several times. Only what it prints is checked.
In Python, use == for text. In Java, use the equals method.
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 7 times: the 4 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
correct username and password (rule 2)
You type
- typed: admin
- typed: secret
It prints
User: admin Welcome, admin
correct username, wrong password (rule 3)
You type
- typed: admin
- typed: wrong
It prints
User: admin Wrong password
wrong username, right password (rule 4)
You type
- typed: guest
- typed: secret
It prints
User: guest Unknown user
wrong username, wrong password (rule 4)
You type
- typed: guest
- typed: wrong
It prints
User: guest Unknown user