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.
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:
Do you know how to add a number to a list? You can use the .append method, e.g. number.append(1).
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.
Test your program to ensure that it is easy to use and gives the correct answers.
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.
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].