CSC11 Homework 10 Solution Programs 2014
--D. Thiebaut (talk) 06:53, 23 April 2014 (EDT)
Program 1
# hw10a.py
# Joyce Xue ( aa )
# (slightly edited by D. Thiebaut)
# 04.15.2014
#
# This program contains a class Cat that describe a cat with its name, age,
# being vaccinated or not, being neutered or not and breed
#
# To create an object of this type, simply use the constructor, as follows:
#
# cat = Cat(name, age, vaccinated, neutered, breed)
#
# The methods supported are:
#
# isVaccinated(): returns if the cat is vaccinated
#
# isNeutered(): returns the name of the planet with the largest diameter
#
# __str__(): convert the object to a string
# ------------------------------------------------------------------------------------------------------
class Cat:
# contructor. Initialize all memebers.
def __init__( self,name, age, vaccinated, neutered, breed):
self._name=name
self._age=age
self._vac=vaccinated
self._neu=neutered
self._breed=breed
#==================================================================
# __str__() : When call print(cat), this function
# automatically converts the cat to a string
#==================================================================
def __str__(self):
# make up the return string by following the required format
temp = "Cat: "+self._name+","+" age: "+str(self._age)+", "
# convert boolean value to string
if self._vac: temp=temp+"vaccinated, "
else: temp=temp+"not vaccinated, "
if self._neu: temp=temp+"neutered "
else: temp=temp+"not neutered "
temp=temp+"("+self._breed+")"
return temp
#==================================================================
# isVaccinated(): return if the cat is vaccinated
#==================================================================
def isVaccinated(self):
return self._vac
#==================================================================
# isNeutered(): return if the cat is neutered
#==================================================================
def isNeutered(self):
return self._neu
#==================================================================
# getNewCat(): ask for user input to create a new
# cat and return it
#==================================================================
def getNewCat():
# user input for name, age, if vaccinated, if neutered and breed
print( "\nPlease enter the information for the new cat:")
name = input( "Cat name? " )
age = int( input( "Age, in years? " ) )
vac = input( "Vaccinated (Y/N)? " ).strip().lower()
vac = vac in ['y', 'yes']
neut = input( "Neutered (Y/N)? " ).strip().lower()
neut = neut in ['y', 'yes' ]
breed= input( "Breed? " ).strip()
return Cat( name, age, vac, neut, breed )
#==================================================================
# display(): print each cat in a Cat collection
# L - a list of cats
# caption - the subject of the list to be printed
#==================================================================
def display( L, caption ):
# print the subject
print( "\n\n"+caption )
print( "=" * len( caption ) )
# go over the list and print each cat's info
for cat in L:
print( cat )
#==================================================================
# main():
# * creates a list of cat objects, displays it.
#
#==================================================================
def main():
# inital cats' info
text="""Minou, 3, Yes, Yes, stray
Max, 1, Yes, No, Burmese
Gizmo, 2, No, No, Bengal
Garfield, 4, Yes, Yes, Orange Tabby"""
# use the text as the initial data, generate cats
for line in text.split( "\n" ):
# split each line into items
words = line.strip().split( "," )
# if data is not exactly 5, skip
if len( words ) != 5:
continue
# set items to variables
name, age, vaccinated, neutered, breed = words
# convert age to int
age = int( age.strip() )
# convert user inputs to booleans
if vaccinated.strip().lower() == "yes":
vaccinated = True
else:
vaccinated = False
neutered = neutered.strip().lower() == "yes"
# construct a cat
cat = Cat( name.strip(), age, vaccinated, neutered, breed.strip() )
try:
# add the cat to the list
cats.append( cat )
except NameError:
# if the cats list is not initialized, initialize it and append the first cat
cats = []
cats.append( cat )
# output the cat list
display( cats, "List of cats:" )
# let user input a new cat's info and append it into the list
cats.append( getNewCat() )
# output the list again
display( cats, "List of cats with new addition:" )
# -------------------------------------------------------------------
# Code Addition
# -------------------------------------------------------------------
# Go through each cat and add those who is both vaccinated and neutered into a list
vaccinatedNeuteredCats = []
for cat in cats:
if cat.isVaccinated() and cat.isNeutered():
vaccinatedNeuteredCats.append( cat )
# output the cats in this new list (both vaccinated and neutered)
display( vaccinatedNeuteredCats, "Vaccinated and neutered cats:" )
# Main entry point for the program
main()
Program 2