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.
- If the command is
help, printCommands: help, quit. - If the command is
quit, printGoodbye. - For any other command, print
Unknown:followed by the command. - Print exactly one of those three lines. Spaces at the end are ignored.
- The comparison is case sensitive:
Helpis not the same ashelp.
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
- typed: help
It prints
Commands: help, quit
the quit command (rule 2)
You type
- typed: quit
It prints
Goodbye
an unknown command (rule 3)
You type
- typed: hello
It prints
Unknown: hello