Real programs are made of the small ideas that you already know, put together. In this course you plan and build three programs: a guessing game with a score, a quiz that marks your answers, and a grade book with a report. Each project starts with a plan and a table of expected answers, is built in small steps, keeps the thinking in small functions so that it can be tested, and ends with ideas for you to extend. A last lesson gives a checklist for finding the fault in your own programs, and shows where to go after the Foundations. The practice tests the functions of each project on many cases and tells you which ones fail.
4 lessons, about 2 h4 simulations14 problems8 practice questionsPython in every lesson
A guessing game is built by planning first. The computer picks a secret number with random.randint, the player has a limited number of tries, and each wrong guess gets the hint Too low or Too high. The thinking goes into two small functions, judge and score_for, that have no input or print, so they can be tested alone. The loop continues while there are tries left and the player has not won, and a flag variable remembers the win. Test the win on the first try, the win on the last try, and the loss.
A quiz keeps its questions in one list and its answers in another, in the same order, so that the same position belongs together. A loop goes through the positions with range of len, asks each question, and compares the reply with the answer. The comparison is a small function that ignores capital letters and spaces at the ends. A second function gives the percentage. If the two lists are not the same length, the loop reaches a position that one list does not have and Python shows an IndexError. Test the function with several replies, and extend the quiz with more questions and messages.
A grade book turns raw marks into a report. The names and the marks are in two lists with the same position for the same student, and two small functions do the thinking: grade gives a letter for a mark, and average gives the mean of a list. A loop prints a line for each student with the columns lined up by f-string widths, then the class average and the highest mark, and marks.index finds the position of the highest mark, so names at that position is the name. Test the grade function at the edges. Extend the grade book by counting how many students got each grade.
A program that does not work is normal, and finding the fault is a skill. A checklist helps: read the error message from the bottom, find the line, reproduce the fault with the smallest input, follow the program by hand with a trace table, print the values inside the loop, check the edges, and change one thing at a time. When the program runs but the answer is wrong, compare with the expected answer worked out by hand. Keep the thinking in small functions so that each can be tested alone. After the Foundations, the next steps are dictionaries, tuples and sets, handling errors with try and except, modules, and larger projects in the Essentials level.
Log in to join the discussion.
Loading…