A menu-driven program allows users to buy birds from a bird shop called Twitter.
The program processes:
Only 3 varieties of birds are currently available:
Here is a prototype of the code.
Open your IDE and type up the program. Can you get it working?
Test that the program works by:
If you need to include more print() statements to help you see what is going on, do so.
Try to buy a crow. The system should tell you that the shop does not sell that kind of bird.
Test the program by:
Enter a starting balance of 0.
Try to buy a bird. The system should tell you that you do not have enough money.
This program uses local variables eg balance and the stock list.
If another function needs the data stored in the variables, we can pass the data to the function when we call it.
Designing programs like this make your functions less reliable on global variables.
Using global variables reduces the portability of your code if you want other people to use your functions in their programs. Can you see why?
It gets awkward if a program requires a particular global variable just so that a function can work correctly.
Try to keep your use of global variables to a minimum!
One tip is to make a list of things that your program needs to do before you start coding - you could do this as comments in Python eg
Notice how useful the comments in the code are.
Especially in programming teams, comments are important because they allow other programmers to quickly understand the purpose of a line or block of code.
If your code is getting complex, comments can remind you what your code is supposed to do, too!