Schedule > Tutorial 5. Error Handling
Background
In preparation for Tic Tac Toe, we are going to help you get started by helping you to handle runtime errors. As you may recall, runtime errors typically happen when your program encounters data that was not adequately anticipated by the programmer. Examples of runtime errors include encountering (a) data with the wrong data type (e.g. a string instead of an int), or (b) data that doesn’t make sense for the given context.
Error Handling in HW5
In Homework 5, you will be creating a Tic-Tac-Toe game where a user will play a computer in a game of Tic-Tac-Toe. One critical part of engineering the game is making sure that your user enters the right input. Specifically:
- The user must enter an integer between 1-9.
- The user cannot enter a number corresponding to a slot that has already been taken. For instance, in the scenario below, if the user (represented by the symbol
o
) types the number5
, the program should refuse the request and explain to the user why their move is illegal (because the 5-slot is already take byx
).| | 7 | 8 | 9 ——————————— ——————————— x | x | 4 | 5 | 6 ——————————— ——————————— | o | 1 | 2 | 3
Your Task
Please make the following enhancements to the tictactoe_starter.py
file (which correspond to the ability for the user to play their move):
- If the user does not enter a number from 1-9, tell them their entry was invalid and that they should try again (use try/except as well as if/else).
- If the user enters a valid number that is not empty, tell them that slot is already taken.
- If the user types “Q”, terminate the program.
- Keep re-prompting the user until they enter a valid number (or Q). See sample video:
What to turn in (same deal as always)
As described in the syllabus, there are two ways to earn full participation credit in each tutorial session:
- By attending them (synchronously), working through the exercises, and asking questions (as they arise).
- By turning in the tutorial exercise(s) ON CANVAS before the tutorial session.
Whichever option you choose, you will be expected to know the material reviewed in the tutorial for the exam, and are encouraged to fully complete and submit the tutorial assignments (though this is not required if you attended the tutorial).