Difference between revisions of "CSC111 Lab 10 2014"
(→Challenge 7) |
(→Challenge 2) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 98: | Line 98: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | '''Note''': you may be tempted to code the output as follows: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | print( "%20s %10.2f %10.2f" % ( name, pocket, debt ) ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | but it may be easier to decompose it in several steps: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | s1 = "%20s" % name | ||
+ | s2 = "%10.2f" % pocket | ||
+ | s3 = "%10.2f" % debt | ||
+ | print( s1, s2, s3 ) | ||
+ | </source> | ||
<br /> | <br /> | ||
Line 209: | Line 224: | ||
[[Image:dogCartoon.jpg|200px|right]] | [[Image:dogCartoon.jpg|200px|right]] | ||
<br /> | <br /> | ||
− | * Using the '''Person''' class as an example, create a new class that will be called '''Dog''' | + | * Using the '''Person''' class as an example, create a new class that will be called '''Dog'''. This class will contain some information one may find useful to associate with a dog, i.e.: |
− | ** tag name (string) | + | ** a tag name (string) |
− | ** age (int) | + | ** the age (int) |
− | ** vaccinated (boolean) | + | ** whether it is vaccinated (boolean) |
− | ** breed (string) | + | ** the breed (string) |
<br /> | <br /> | ||
*Add a dog to the list using the simplest way, illustrated below: | *Add a dog to the list using the simplest way, illustrated below: | ||
Line 261: | Line 276: | ||
Samson, 3 years old, vaccinated, Great Dane | Samson, 3 years old, vaccinated, Great Dane | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Submission= | =Submission= | ||
<br /> | <br /> |
Latest revision as of 09:41, 10 April 2014
--D. Thiebaut (talk) 13:39, 7 April 2014 (EDT)
This lab presents some exercises on exceptions and Object-Oriented Programming.
Contents
Exceptions
If you feel you have a good handle on exceptions, then move on to the next section. Otherwise do the exercises of this section.
TypeError Exceptions
The code section below will be our testbed for this part of the lab. You have to make it compute the different values we are interested in without using if/else/elif statements, using instead try/except statements.
# happiness.py
# Lab 10 demo
SWsFriends = [
# name pocket-money debt
("Sleepy", 10, "NA" ),
("Sneezy", 25.50, 0 ),
("Bashful", 0.50, 10.51 ),
( "Happy", 0, 0 ),
( "Grumpy", 0, 100.30 ),
( "Dopey", "NA", 0 ),
( "Doc", 200, 0 ) ]
def main():
for name, pocket, debt in SWsFriends:
print( name, "has", pocket, "dollar(s) in his pocket, and owes",
debt, "dollar(s)" )
main()
- Run the program.
- Notice that some amounts of money have "NA" as a value (Not Available).
- Add a for-loop to your program that will add up the total amount of money Snow White's friends have in their pocket.
Do not anticipate errors. Just go for it! Assume there won't be errors and put all the code necessary to compute the sum of all the numbers in the 2nd column.
- When you run your program the first time, you will get a TypeError because you cannot take the float( ) of the string "NA". Add a try/except clause around the addition, as illustrated below:
try:
sumPocket += pocket
except TypeError:
continue
- Run your program again. Verify that you get the total sum in all the pockets (we assume that NA will be equivalent to 0).
total sum in all the pockets: 236.0
Challenge 1 |
- Add a new loop that computes the total amount of debts held by the 7 friends.
- Do not use if statements. Use a new try/except clause to protect your code from exceptions.
Challenge 2 |
- Make your program print the contents of the list in 3 columns, as shown below. Use a print format containing "%f" type commands for the floats:
Sleepy 10.00 NA
Sneezy 25.50 0.00
Bashful 0.50 10.51
Happy 0.00 0.00
Grumpy 0.00 100.30
Dopey NA 0.00
Doc 200.00 0.00
Note: you may be tempted to code the output as follows:
print( "%20s %10.2f %10.2f" % ( name, pocket, debt ) )
but it may be easier to decompose it in several steps:
s1 = "%20s" % name s2 = "%10.2f" % pocket s3 = "%10.2f" % debt print( s1, s2, s3 )
Be Friendly! Print an Error Message for the User!
When you catch an exception, the catch TypeError type of statement indicates some like "if there is an exception of type TypeError, then... But that does there is actually an object of type TypeError, an we can ask Python to give it to us, if we want to. The way to do this is to write:
except TypeError as err:
...
in this case, err is the object of type TypeError. Most objects of this type have a member variable called args, which is a list of arguments, and which includes the description of the error in the first location. So to print it, one would simply do this:
except TypeError as err:
print( "*** ERROR ***", err.args[0] )
The full program would look something like this:
SWsFriends = [
# name pocket-money debt
("Sleepy", 10, "NA" ),
("Sneezy", 25.50, 0 ),
("Bashful", 0.50, 10.51 ),
( "Happy", 0, 0 ),
( "Grumpy", 0, 100.30 ),
( "Dopey", "NA", 0 ),
( "Doc", 200, 0 ) ]
sumPocket = 0
for name, pocket, debt in SWsFriends:
try:
sumPocket += pocket
except TypeError as err:
print( err.args[0] )
continue
print( "total sum in all pockets: ", sumPocket )
Challenge 3 |
- Update your program and make it print the error string associated with the exception whenever one occurs.
Object Oriented Programming (OOP)
Part 1: List of People
- Create a copy of the OOP program we saw in class
- Run it. Make sure you understand how the code works, and that you understand its output.
Challenge 4 |
- Modify the program and make it ask the user for the information about a new student, and add this student to the list. Print the list at the end.
Challenge 5 |
- Play with this code in the console:
>>> L = [30, 50, 20, 10, 100] >>> L.pop( 2 ) >>> L
- Once you figured out how pop() works on a list, make your program ask for a student name, and then remove (pop) that student from the list.
Part 2: A List of Dogs
- Using the Person class as an example, create a new class that will be called Dog. This class will contain some information one may find useful to associate with a dog, i.e.:
- a tag name (string)
- the age (int)
- whether it is vaccinated (boolean)
- the breed (string)
- Add a dog to the list using the simplest way, illustrated below:
dogs.append( Dog( "Rex", 4, True, "German Shepherd" ) )
- Make your program display this list of one dog. This will require your having created a method called toString() that will return a string with the dog information in it. Here's an example of a possible string returned by this method:
'Rex, 4 years old, vaccinated, German Shepherd'
- When this works, add three more dogs to the list. Make some of them vaccinated, some not vaccinated. Also make a couple of dogs the same breed (In case you need help finding breeds, check this page out). Make sure the program displays the list correctly.
Challenge 6 |
- Add some code to your program that will display only the dogs that are vaccinated.
- Add some more code that will display all the dogs of a given breed. Pick the breed that you know has more than one representatives in your list.
Below is an output showing a possible way for your program to behave:
List of 1 dog Rex, 4 years old, vaccinated, German Shepherd List of 4 dogs Rex, 4 years old, vaccinated, German Shepherd Fifi, 1 years old, not vaccinated, Poodle Samson, 3 years old, vaccinated, Great Dane Mad Max, 2 years old, not vaccinated, Poodle The vaccinated dogs are: Rex, 4 years old, vaccinated, German Shepherd Samson, 3 years old, vaccinated, Great Dane Please enter a breed: Great Dane Dogs of breed Great Dane Samson, 3 years old, vaccinated, Great Dane
Submission
Submit the program (which you should name lab10.py) to this URL: http://cs.smith.edu/~thiebaut/111b/submitL10.php