HW3: Cat Sim Python Module

Verified

Added on  2019/10/18

|6
|1347
|68
Practical Assignment
AI Summary
This practical assignment requires students to create a Python module (`hw3.py`) containing a `Cat` class with at least eight methods: `__init__`, `__eq__`, `__str__`, `__repr__`, `feed`, and three additional methods of their choice. The `Cat` class must track the total number of cats, and each cat instance needs attributes for name, color, favorite food (defaulting to "Meow Mix"), `is_hungry` (initially True), and `happiness` (starting at 1). The assignment also involves creating a `main` function that simulates a cat adoption and interaction system. User input drives the simulation, with actions like adopting, feeding, and quitting. All console output and user input are logged to a `log.txt` file. A `README.txt` file documents the user inputs and corresponding Python code calls. The `__repr__` method is called for all cats when the user inputs 'census'. Bonus points are awarded for creativity in the additional methods.
Document Page
Regarding the code
Write a Python module in hw3.py that includes the following:
class Cat:
"""An object of class Cat will have a minimum of 8 methods: __init__,
__eq__, __str__, __repr__, feed, and at least three methods of your
choice.
The Cat class has to keep track of the total number of cats owned as
num_cats.
Each cat must have a name, a color, and optionally a favorite food that
defaults to "Meow Mix." Each cat also has a boolean attribute variable
called is_hungry which is initially True and an attribute happiness: an
int
that represents the cat's level of happiness (higher = happier) and
starts
at 1. Cats may also have as many optional parameters and variables as
you
would like.
"""
def __init__(self, name, color, fav_food="Meow Mix"):
"""Initialize a Cat object, saving as instance attributes its name, color,
favorite food (defaults to "Meow Mix"), and any other attributes you
would
like; and prints a message saying that a new cat has been acquired and
what
its name is.
Parameters:
self
name: String
color: String
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
fav_food: String -- is "Meow Mix" if not given
Etc. of your choosing(Please remove this line and document your
own
parameters here)
age: Int -- representing age in years of the Cat ##not required, just
example
"""
def __eq__(self, other):
"""Compares two Cats to test equality. Cats should be equal if their
names
and colors are the same.
Parameters:
self
other: Cat object -- the second Cat you want to use
Return: Boolean -- True if the Cat objects are equal, False otherwise
Usage Examples:
>>> cat = Cat("Sprinkles", "white")
>>> other_cat = Cat("Princess Lady", "white", "Fancy Feast")
>>> cat == other_cat
False
"""
def __repr__(self):
"""Describe the Cat object. Return a string that lists the pairs of
attribute/values for that Cat. Including: name, color, is_hungry,
fav_food,
age, happiness, etc. (Remember: you can add attributes for creativity's
sake or to make your additional methods work.)
Parameters:
self
Document Page
Return:
info_list: String -- Start with a list of tuples where each tuple has 2
items, an attribute followed by its value. Then convert to a string and
return it.
Usage Examples:
>>> cat = Cat("Garbage", "grey") ### It is OK if this fails after you
add additional attributes to your cats.
>>> cat ### Just maintain the same format for
additional attributes as well
[('name', 'Garbage'), ('color', 'grey'), ('fav_food', 'Meow Mix'),
('is_hungry', True), ('happiness', 1)]
"""
def __str__(self):
"""Gives an aesthetically pleasing representation of the referenced cat
object. All cats will have the same representation.
Parameters:
self
Return: A String containing a cat emoji (of your choosing) and a newline
character and the cat's name
Usage Examples:
>>> cat = Cat("Sprinkles", "white")
>>> print(cat)
=^.^=
Sprinkles
"""
def feed(self, food):
"""Give food to a Cat, changing its is_hungry value to False, and when
given
Document Page
its favorite food, a cat's happiness will increase by 1.
This method should print that the cat has been fed and if the cat is fed
its
favorite food, an additional message will say that the cat is noticeably
happier.
Parameters:
self
food: String -- the food being given to the cat
Return: None
Usage Examples:
>>> new_cat = Cat("Princess Lady", "white", "Fancy Feast")
>>> new_cat.feed("Fancy Feast")
Princess Lady ate Fancy Feast and is no longer hungry.
Princess Lady seems happier!
"""
Write at least 3 more methods for Cat objects with documentation.
Be creative and have fun. Bonus points can be awarded at the
discretion of your TA for exceptional creativity.
Main
def main(args):
"""Main asks for a username using user input.
Asks <username> to begin the game and uses a while loop to
continuously
accept user input until user quits the sim with 'quit'.
'quit' and 'census' are user input calls that will end the sim and call
the __repr__ method for ALL cats, respectively.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Every output printed to the console and everything typed by the user
should
be saved to a text file named log.txt while the main loop is running.
"""
User input strings will perform functions and methods written in the
script without using classic function calls, so an example would be:
>>>Welcome to Cat Sim v1.0. What is your name? Jon
Hello Jon.
>>>What would you like to do next? Adopt
>>>Congrats! What's your new cat's name? Garfield
>>>What color? Orange
>>>What's its favorite food? lasagna
Garfield is now happily in your home!
>>>What would you like to do next? Feed Garfield
>>>Feed what food? lasagna
Garfield has been fed and is no longer hungry
That food was great! Garfield looks happier than before!
>>>What would you like to do next? Quit
Thanks for playing, Jon!
So the first line of log.txt should be 'Welcome to Cat Sim v1.0.
What is your name?" and the last line would be"Thanks for
playing, Jon!"
The following may be helpful for capturing print statements to the
console:
Write a print statement whenever necessary.
o Immediately after the print statement, append the same
output to log.txt.
When the user types to the console, append their input string
to log.txt.
o Then respond appropriately to whatever that input was.
When your program ends, be sure that your log.txt has been
closed.
Document Page
If the user inputs ‘census’, the __repr__ method is called on all
instances of cats.
Additional Documents
README.txt
A text file containing the instructions to your sim. Each line in the
text document should have the user input followed by the Python
code it calls. Don’t forget that ‘census’ and ‘quit’ are required for
all README.txt files.
Example:
adopt = Cat(userin("Congrats! What's your new cat's name?"),
userin("What color?"), userin("What's its favorite food?"))
feed <cat> = <cat>.feed(userin("Feed what food?"))
census = <cat> … ##for all cats that exist
Submission Instructions
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]