Twitter

 

A menu-driven program allows users to buy birds from a bird shop called Twitter.

The program processes:

  1. the user's balance
  2. the variety of the available birds
  3. the price of the available birds
  4. the stock numbers of the available birds

Only 3 varieties of birds are currently available:

  1. canary
  2. pigeon
  3. bald eagle

Here is a prototype of the code.

Task 1 - Type it up

Open your IDE and type up the program. Can you get it working?

Task 2 - Testing phase A

Test that the program works by:

  • Selecting each menu item, 1, 2, 3, 4. Does the program work as expected?

If you need to include more print() statements to help you see what is going on, do so.

Task 3 - Testing phase B

Try to buy a crow. The system should tell you that the shop does not sell that kind of bird.

Task 4 - Testing phase C

Test the program by:

  • displaying the menu - check that there is one pigeon in stock
  • buy the pigeon, check that your balance is updated and display the birds' info - the pigeon stock should be 0
  • try to buy another pigeon. This should no longer be possible.

Task 5 - Testing phase D

Enter a starting balance of 0.

Try to buy a bird. The system should tell you that you do not have enough money.

Task 6 - Code Analysis

Edit this PDF file by answering the questions relating to the code.

Food for thought

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!

Comments

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!