Course 4.2Lesson 4 of 5
A command menu joins the tools into one program that reads commands from the user, such as add asha 98765, find asha and list. A dictionary of functions maps each command word to the function that does the job, so adding a command means adding one function and one entry. The loop reads a line with input, splits it into the command and its arguments, refuses an unknown command, ignores an empty line, and leaves on quit. Catching an EOFError ends the program cleanly when the input has run out. Each command function checks its arguments and answers with a message, and the loop only prints it. This design keeps the tools testable and the menu small.
Try the code and the simulations beside the lesson.
After this lesson you can
if chain,You now have three tools. A real program lets a person use them: type a command, see the answer, type another. This is a command menu, the heart of many programs, from a shell to a game.
The design has three layers.
Because the layers are separate, you can test each one alone.
Functions are values in Python. They can be kept in a list, and in a dictionary. This makes a neat table: the key is the command word, and the value is the function that does it.
Type these lines in the Input box, one on each line, and run it:
add asha 98765
add kabir 12ab
find asha
find zoya
list
colour red
quitPredict first. Which lines give an error message, and what does the list show at the end?
Read the program.
commands = {"add": do_add, "find": do_find, "list": do_list} is the table. Notice that there are no brackets after the function names: we store the function itself, not its answer.commands[word](parts[1:]) looks up the function and calls it. Read it from left to right: get commands[word], then call it with the arguments.if chain that you would write otherwise grows with each command.usage: add NAME NUMBER), and returns a message. It does not print, so the loop is the only place that prints.if not parts: continue), says unknown command for a word that is not in the table, and leaves on quit.input("> ") is inside a try. When the input runs out, Python raises an EOFError. We catch it and break. This is the clean way to end when the Input box has no more lines, or when a file of commands finishes.In this small program, contacts is a variable of the main program, and the command functions use it directly. That is simple, and good for a short program. In a bigger program, you would pass the book to each function as a parameter, as you learned in the course on functions, so that each function works alone. Choose by size: a short menu can share one dictionary; a long one should pass the data.
To add a command, write one function and add one entry. The loop that runs the menu never changes.
A menu is harder to test than a pure function, because it reads the keyboard. The trick is to test the command functions and the tools alone, with the table of tests, and to test the loop by hand with a list of commands. A good script of commands has:
| Command | Tests |
|---|---|
| a normal command of each kind | each function works |
| a command with too few arguments | the usage message |
| a command with a bad argument | the check |
| an unknown command | the message |
| an empty line | nothing happens |
quit | the program ends |
no quit, just the end of the input | the EOFError is caught |
"add": do_add() calls the function at once. Write do_add.print(commands[word](...)) shows None.IndexError.EOFError. The program stops with an ugly message when the input ends.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
remove command and a count command to the menu above.quit.EOFError, so that the program ends cleanly when the input ends.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
commands["add"]?EOFError mean here?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.