Skip to content
ExamSpine

Lesson 2.6 · Write it

Command 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 command (text) and responds.

  1. If the command is help, print Commands: help, quit.
  2. If the command is quit, print Goodbye.
  3. For any other command, print Unknown: followed by the command.
  4. Print exactly one of those three lines. Spaces at the end are ignored.
  5. The comparison is case sensitive: Help is not the same as help.

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

In Python, use == and elif. In Java, use .equals and else if.

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 help command (rule 1)

    You type

    1. typed: help

    It prints

    Commands: help, quit
  • the quit command (rule 2)

    You type

    1. typed: quit

    It prints

    Goodbye
  • an unknown command (rule 3)

    You type

    1. typed: hello

    It prints

    Unknown: hello
solution.pySolution.java