Using Lists

In this assignment we are going to use the PRIMM approach to help you to develop a new program by modfying an existing one.  PRIMM stands for Predict, Run, Investigate, Model, Make and we looked at the first three stages in the lesson.  If you missed the lesson, then you can watch the recording or look at the questions below in the Predict, Run, Investigate section.

Predict, Run, Investigate

If you attended the lesson then we have already discussed these questions.

Look at the this example program in Basthon.  You can copy and paste the code into another IDE such as IDLE, Trinket or Replit.

Before you run the code:

Now investigate the program by asking yourself these questions:

  1. Run the program. 
  2. What does the str() command do on line 3?  Why is it necessary?
  3. What type of data is stored when you use the input() command?
  4. Can you update the program so that each time the user enters a number, the program displays what one more than that number would be?  For example, if you enter 1, then the program displays 2?
  5. At the end of the program, would be it be possible to calculate the total of all the numbers entered?  Why not?
  6. Do you know how to add a number to a list?  You can use the .append method, e.g. number.append(1).

Modify

Make a copy of the program in replit.com and make as many of these changes as you are comfortable with.  As you attempt more tasks you will become more confident and will complete more of the steps in future tasks.

  1. Update the program so that it asks the user for five numbers.
  2. Convert the user's input to a number.
  3. Store all of the numbers in the numbers lists as the user enters them.
  4. Sort the numbers into order and print the smallest and biggest numbers.
  5. Print the total of the numbers entered.
  6. Print the mean average of the numbers entered.

Test your program to ensure that it is easy to use and gives the correct answers.

Extension

If you found that quite straightforward, why not generalise it?  See if you can modify your program so that it can cope with users entering different numbers of values rather than always asking for five.  How could you do that?  I can think of at least two ways to do it - you could ask how many numbers there are, or you could look out for a particularly value, e.g. 0, being entered and then stop.

Help

Remember that the names of the different types in Python - int, str, bool and float - are also commands that you can use to convert between types. For example, str(123) will turn 123 into the string "123", and int("1") will turn "1" into the number 1.

There are various things that you can do with a list, e.g. if you have a list called numbers then:

Finally you can use the index to pick out an individual item from a list.  Each item is numbered from the left, starting at 0, so the first item of numbers will be numbers[0].  We can also use negative numbers to count in from the right, so the last item in numbers is numbers[-1].