This document contains the instructions for the week 3 CSC148H/A48H lab. To earn your lab mark, you must actively participate in the lab. We mark you in order to ensure a serious attempt at learning, not to make careful critical judgments on your work.
The lab this week is about programming with exceptions.Pick a navigator and driver.
Download the file stack.py. Modify the file
as follows:
StackEmptyError
.__str__
method for StackEmptyError
that returns the message "Tried popping an empty stack". pop()
method in the Stack
class so that it raises StackEmptyError
if
pop()
is called when the stack is empty.
login(password)
- logs caller into a secure system
using the password
string. Raises PasswordError
exception if the password is wrong. PasswordError
is raised by the login function, the
designer thought it would be helpful to provide information about the
position in the attempted password at which the first incorrect
character appears. In particular, PasswordError
has the
following method:first_wrong_pos()
- returns the first incorrect
position
in the attempted password that caused the PasswordError
to
be raised. All the characters prior to the first incorrect position
match the characters in the correct password. first_wrong_pos()
will return 3. login()
method so that it doesn't raise a PasswordError
exception. You may assume that the
password only contains capital letters A to Z (no numbers,
spaces, punctuation marks, etc).update_guess(guess, pos)
- returns a modified
version of guess
in which the character at position pos
is changed to the next letter in the alphabet. The return value is
undefined if you try updating a position with the character "Z".
(E.g., if you call update_guess("HELLO",1)
, the returned
string will be "HFLLO"
.) update_guess()
only modifies existing characters in your
guess -- it does not add new characters. If you want to increase the
length of your guess, you can do so with string concatenation (i.e.,
using the '+'
operator). (For example, if s1
and s2
are strings, then s = s1 + s2
is
the string that consists of s1
followed by s2
.)
EnrollmentError,
PrerequisiteError, CourseClosedError,
and
CourseCancelledError. EnrollmentError
is a superclass of all the
other exceptions. enroll.py also contains the function enroll(student,
course),
which enrolls the student
in the course.
raw_input("Student name: ")
and raw_input("Course
name: ")
to do this. Have your code enroll the student in the
course by calling the enroll
function, enclosing
the call in a try clause. If any of the previously mentioned exceptions
are raised in the enroll
function, print the exception
object and re-ask the user for a student name and course name. Keep
trying to enroll the student in the course until either enroll
raises no exceptions or the user enters an empty string ('') for one of
the inputs. Exceptions other than those mentioned in the previous
paragraph should not be caught.except
statements that you use. In particular, keep in mind that EnrollmentError
is a superclass of the other exceptions listed in the first paragraph. CourseClosedError
is
raised, your program asks the user for a new course name but not a new
student name. Your code should then try enrolling the same student in
the new course. If any of the other exceptions are raised, your code
should behave as before (i.e., ask for both a new student name and
course name.)