Consolidation

Task 1 - List Practice!

The second task (Task 1b) is challenging, so choose that one if you are feeling confident!


Task 1a: Using a list

This task is in several stages:

  • Use a for loop and a list to input 6 animal names from the keyboard. Print the list out on the screen.
  • Extend your program to ask the user whether they would like the list printed out in the original order or in reverse order.
  • Extend your program further to ask the user to choose a number between 1 and 6 and to print out that item in the list, where the first item in the list is the first item (not item 0).

Task 1b: Making a tally chart

We want to simulate throwing a die 30 times and record the scores. If we did this manually we would end up with a tally chart:

tally chart

If we use a computer to keep a count of how many times each number was thrown we could use a list containing 6 integers to simulate the manual tally chart.

In general a die thrown will give a score we can call score (where score can be in the range 1 to 6) and we will need to increment the count in element (score-1) of the list using:

  • TallyChart[score-1] + 1

(Remember the first element in a list is element zero.)

Write a program to simulate the throwing of a die 30 times. The results of the simulation should be printed as a table of scores and frequencies.

Hint: use the function randint in the random library to generate a random number between 1 and 6.

You will need to import the random library into your program.