Skip to content
ExamSpine

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.

  1. Always print User: followed by the username on the first line.
  2. If the username is admin and the password is secret, print Welcome, admin.
  3. If the username is admin but the password is wrong, print Wrong password.
  4. If the username is anything else, print Unknown user.
  5. 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

    1. typed: admin
    2. typed: secret

    It prints

    User: admin
    Welcome, admin
  • correct username, wrong password (rule 3)

    You type

    1. typed: admin
    2. typed: wrong

    It prints

    User: admin
    Wrong password
  • wrong username, right password (rule 4)

    You type

    1. typed: guest
    2. typed: secret

    It prints

    User: guest
    Unknown user
  • wrong username, wrong password (rule 4)

    You type

    1. typed: guest
    2. typed: wrong

    It prints

    User: guest
    Unknown user
solution.pySolution.java