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:
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.
- 1. Introduction to Python
- 2. Data types and Operators
- 3. String Operation and Math Unit Exercises
- 4. The IF Statement
- 5. More on IF
- 6. The Maze Game
- 7. Selection Exercises
- 8. FOR Loops
- 9. While Loops
- 10. Making a quiz
- 11. Iteration Exercises
- 12. Lists, Functions and Files
- 13. Creating a list
- 14. Lists with a FOR Loop
- 15. List operations
- 16. Consolidation
- 17. Lists Exercises
- 18. Functions
- 19. Parameters
- 20. Functions with parameters
- 21. Menu Program
- 22. Functions Exercises
- 23. Reading from a file
- 24. Reading from a file - Part 2
- 25. Writing to a file
- 26. Extending the Quiz