Python by Projects › PET SIMULATOR
Exercises: PET SIMULATOR
Adopt a virtual pet with its OWN name and mood. Feed it, play with it, and watch its stats change. Build each part of your simulator in the blank code cells.
Exercise 1: Make a Pet
Define class Pet:.
Give it def __init__(self, name): and set self.name = name, self.hunger = 5, and self.happiness = 5.
Make buddy = Pet("Buddy"), then print buddy.name and buddy.hunger.
Real program
After the fixed version works, read the pet's name with input(), construct the pet from that name, and keep both prints.
How this is checked: Checked by tests that run your code.
Exercise 2: Two Pets, Two Stories
Define the Pet class again with the same __init__ method and three attributes.
Make two pets with different names.
Use a while loop to lower only the first pet's hunger by 1 until it reaches 2.
Then print both names and both hunger values.
Your result should show that each object carries its own attributes.
Real program
After the fixed version works, read both pet names with separate input() calls. Keep the hunger loop and all four printed values unchanged.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 3: Add a Feed Method
Define the Pet class again with its __init__ method.
Inside the class, add def feed(self, amount):.
Make it change hunger with self.hunger = self.hunger - amount, then return the new hunger.
Make Buddy, call buddy.feed(2), and print the returned value.
Real program
After the fixed version works, read the pet's name and feed amount, using int(input()) for the amount. Construct, feed, and print with the values you read.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 4: Build the Mood Ladder
Define Pet with the same three starting attributes.
Add def status(self): and create a variable named mood inside it.
If hunger is greater than 6, use "hungry".
Else if hunger is less than 3, use "stuffed".
Otherwise, use "content".
Finish the method by printing an f-string with the pet's name, mood, hunger, and happiness, then return mood.
Make a pet, change its hunger to 8, and print what its status method returns (it should be "hungry").
Real program
After the fixed version works, read the pet's name and hunger, using int(input()) for hunger. Keep the computed mood, status line, and returned-mood print.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 5: Make a Pet List
Define Pet with __init__ and the full status mood ladder from Exercise 4.
Start with pets = [], then append five new Pet objects.
Use one for pet in pets: loop and call pet.status() once for each pet.
After the loop, write and run these two lines yourself: print(pets[0].name) and pets[1].status().
Real program
After the five-pet fixed version works, read pet names into the list with name = input("Pet name (blank to stop)? ") and while name != "":. Keep the status loop and final two lines. Type at least two names before the blank, so pets[0] and pets[1] both exist.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 6: Feed from Two Foods Tables
Define Pet with __init__ and feed.
Make two different food dictionaries: meals = {"apple": 2, "steak": 4} and treats = {"biscuit": 1, "carrot": 2}.
Add a third entry, "fish": 3, to treats.
Make Buddy, feed Buddy once from meals and once from treats, then print Buddy's hunger and the value stored for "fish".
Real program
After the fixed version works, read the pet's name, the meal key, and the treat key. Use both keys for dictionary access so every read affects the program.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 7: Type a Name
In the regular code cell, define Pet with __init__, use the fixed name "Mochi", and make a pet.
Use a while loop to add 1 to its happiness until happiness reaches 8, then print its name and happiness.
Real program
In the second cell, write the complete real program. Read a name with input("Name your pet? "), construct the Pet from the value you read, run the same happiness loop, and print that pet's name and happiness. The read name must be used; this cell waits for typing, so run it yourself in Jupyter.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 8: Run, Read, and Fix an AttributeError
Run the next code cell yourself.
Its last line has a spelling mistake, so Python will raise an AttributeError.
Read the traceback from the bottom upward and find the misspelled attribute name.
Your traceback: Edit this markdown cell and replace this sentence with the traceback you saw.
Now write the corrected line in the following code cell and print the happiness value.
More Practice
Use Exercises 9–11 for homework or another practice session.
How this is checked: Checked by tests that run your code.
Exercise 9: Feed a Pet Team
In the regular cell, define Pet with __init__ and feed, using five fixed pet names so the cell runs without waiting.
Make a food dictionary and an empty pets list.
Append five pets, then use a for pet in pets: loop to feed each pet from the dictionary.
After the loop, print every pet's hunger with a second for pet in pets: loop.
Real program
In the second cell, write the complete real program. Read every pet name into pets with the blank-name sentinel, feed every pet from the fixed food dictionary, and print every resulting hunger value. Each read name must be used to construct a Pet; run this cell yourself in Jupyter.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 10: Three Snack Stops
Define Pet with __init__ and feed.
Make a snack dictionary and one pet.
Set stops = 0, then use a while stops < 3: loop.
Each time through the loop, feed the pet from the snack dictionary and add 1 to stops.
After the loop, print the pet's hunger and stops.
Real program
After the fixed version works, read the pet's name and read a snack key at each of the three stops. Use each key in snacks[...], then keep both final prints.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 11: Pet Roll Call
Define Pet with __init__ and the full status mood ladder.
The method must use if/elif/else, choose a mood, and print an f-string with the pet's name and mood.
Start with an empty pets list and append five pets.
Use list indexes to set the first pet's hunger to 8 and the second pet's hunger to 1.
Use a for pet in pets: loop to call status() for every pet, then print pets[4].name. The three pets after indexes 0 and 1 stay at the default hunger of 5.
Real program
After the five-pet fixed version works, read pet names into the list with the blank-name sentinel. Keep the two indexed hunger changes, status loop, and final pets[4].name print. Type at least five names before the blank, so pets[0], pets[1], and pets[4] all exist.
In-Class Finale
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 12: Run a Pet Day
In the regular cell, use the fixed name "Buddy" so the whole pet day runs without waiting.
Define the full Pet class with __init__ and all four methods: feed, play, pass_time, and status.
Use the same rules as the lesson: hunger and happiness start at 5; feed subtracts an amount from hunger; play adds 1 to happiness; pass_time adds 2 to hunger; and status uses the three-part mood ladder and prints an f-string.
Make Buddy and call the methods in this exact order: feed(2), play(), pass_time(), status().
Finally, print Buddy's hunger and happiness.
The final printed numbers should be 5 and 6.
Use print to check your work; do not write an assert.
Real program
In the second cell, write the complete real program. Read the pet-day name with input(), construct the pet from that value, call all four methods in the same order, and keep the status and final-number prints. The read name must be used; run this cell yourself in Jupyter.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Algorithm Extension
Extra loop practice — optional enrichment: these drills reuse the same loop shapes on small fixed data, so try as many as you like.
Exercise 13: Happiest Pet
Start with a list named pets that holds several pets with different happiness values.
Set best_pet = pets[0] and best = pets[0].happiness.
Use one for pet in pets: loop.
Inside it, compare with if pet.happiness > best:. When a pet has a new best happiness, update both best = pet.happiness and best_pet = pet.
After the loop, print both best_pet.name and best.
Pattern Spotlight retrieval: Which pattern remembers WHO owns the best value? Variation axis — data shape: objects and their attributes, keeping the pet itself, instead of the earlier number-only file score.
Real program
Read (name, happiness) pairs into pets with the blank-name sentinel, then keep the seeded find-extreme loop and both prints. Type at least one pet before the blank, so pets[0] exists to seed the trackers.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 14: Play Until Happy
Define Pet with __init__ and a play method that adds 1 to self.happiness.
Make buddy = Pet("Buddy").
Use while buddy.happiness < 10: to call buddy.play().
After the loop, print Buddy's happiness.
Pattern Spotlight: which pattern is this? which variable/condition says 'done yet?' Variation — data type and packaging: an object method changes an attribute until it reaches the boundary, unlike the earlier inline number and string loops.
Real program
Read the pet's name, construct the pet from it, and keep the play-until-happy loop and final print unchanged.
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: a number
Exercise 15: List the happy pets
Define Pet with __init__(self, name, happiness) that stores both attributes. Use pets = [Pet("Mochi", 6), Pet("Sunny", 7), Pet("Pip", 9), Pet("Coco", 4)]. Set happiness_threshold = 7 and start a NEW empty list named happy_pets. Loop over pets. Inside the loop, use a condition to check whether each pet's happiness is at least the threshold, and append only passing pet objects to happy_pets. After the loop, use a second loop to print the name of every pet in the new list. The exact printed names should be Sunny and then Pip, one per line. Do not use a list comprehension.
Pattern Spotlight retrieval: Which pattern builds a new list containing only items that pass a test? Variation axis — pet objects and attributes: filter objects by their happiness attribute, then print their names.
Real program
Read (name, happiness) pairs into pets with the blank-name sentinel, then read the happiness threshold with int(input()). Keep both loops and their outputs.
How this is checked: Checked by tests that run your code.
Exercise 16: Count the Hungry Pets
Define a tiny Pet class whose __init__ stores only a hunger attribute. Make your own inline list named pets containing four Pet objects with hunger values 8, 3, 7, and 5, in that order. Use for pet in pets: to count how many pets have hunger greater than 6, then report the count.
Worked example: For these four pets, the answer is 2.
Real program
Read hunger values with input() until a blank entry, convert each to int, construct one Pet per value, and keep the counting loop and report.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 17: Team Hunger Total
Define a tiny Pet class whose __init__ stores only a hunger attribute. Make your own inline list named pets containing four Pet objects with hunger values 8, 3, 7, and 5, in that order. Start a total at zero, use for pet in pets: to add every hunger value with long-form accumulation, and report the total. Use a loop rather than a built-in total shortcut.
Worked example: The four hunger values produce a total of 23.
Real program
Read hunger values with input() until a blank entry, convert each to int, construct one Pet per value, and keep the accumulation loop and report.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 18: Find a Pet by Name
Define a tiny Pet class whose __init__ stores only a name attribute. Make your own inline list named pets containing Pet objects named "Rex", "Mia", and "Sam", in that order. Search for "Mia" with for pet in pets:. Start a found flag as false; when the name matches, set the flag to true and break out of the loop. Report whether the name was found.
Worked example: The result is found = True.
Real program
Read pet names with the blank-name sentinel, then read the target name. Use the read target in the search and in the printed result.
How this is checked: Checked by tests that run your code.
Exercise 19: Pet Roll Call
Define a tiny Pet class whose __init__ stores only a name attribute. Make your own inline list named pets containing Pet objects named "Rex", "Mia", and "Sam", in that order. Start a NEW empty list, use for pet in pets: to append each pet's name, and report the completed names list.
Worked example: The result is ["Rex", "Mia", "Sam"].
Real program
Read pet names with the blank-name sentinel, then keep the loop that builds and prints the roll-call list.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 20: Hungriest and Least Hungry
Define a tiny Pet class whose __init__ stores only name and hunger. Make your own inline list named pets containing Pet("Rex", 8), Pet("Mia", 3), and Pet("Sam", 7), in that order. Seed BOTH the hungriest and least-hungry trackers from the first pet by using list indexing, not zero or a made-up large value. Then use for pet in pets: to scan the objects, update each tracker when needed, and report both pets' names and hunger values.
Worked example: The hungriest pet is "Rex" with hunger 8, and the least-hungry pet is "Mia" with hunger 3.
Real program
Read (name, hunger) pairs into pets with the blank-name sentinel, then keep both seeded extreme trackers and both reports. Type at least one pet before the blank, so pets[0] exists to seed both trackers.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 21: Mood Tally
Define a tiny Pet class whose __init__ stores only a mood attribute. Make your own inline list named pets containing four Pet objects with moods "happy", "sad", "happy", and "hungry", in that order. Start an empty dictionary named counts. Use for pet in pets: and the pattern counts[mood] = counts.get(mood, 0) + 1 to tally the moods, then report the dictionary.
Worked example: The result is {"happy": 2, "sad": 1, "hungry": 1}.
Real program
Read moods with input() until a blank entry, construct one Pet per mood, and keep the .get tally and dictionary report.
How this is checked: Checked by tests that run your code.
Exercise 22: Feed Until the Snacks Would Overfeed
Define a tiny Pet class whose __init__ stores only name and hunger. Make one Pet named Buddy with hunger 8. Make the snack sequence 2, 4, 3, 5, in that order. Hunger has a floor of 0. Move through the snacks in order with a single loop and check before feeding. Apply a snack only if subtracting it would leave hunger at or above zero. Otherwise, break immediately without applying that snack or any later snack. Use long-form subtraction and report Buddy's final hunger and the number of snacks used.
Paired contrast: This mirrors Exercise 23, but here you check the boundary before changing hunger, so the pet never goes below zero.
Worked example: Buddy goes 8 → 6 → 2. The next snack would change hunger by -3 and produce -1, so feeding stops before that snack. The final hunger is 2, and 2 snacks were used.
Real program
Read Buddy's hunger and exactly four snack values with fixed-count int(input()) calls. Keep the check-before-feeding loop so its graded break is the only break.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 23: Feed Until Hunger Goes Negative
Define a tiny Pet class whose __init__ stores only name and hunger. Make one Pet named Buddy with hunger 8. Make the snack sequence 2, 4, 3, 5, in that order. Move through the snacks in order with a single loop and feed then check. Subtract each snack first, count it as used, and then break as soon as hunger is below zero. Start tipping_snack = 0 before the loop. Use long-form subtraction and report Buddy's final negative hunger, the number of snacks used, and the snack value that tipped hunger below zero.
Paired contrast: This mirrors Exercise 22, but here you change hunger before checking the boundary, so the result records the first step below zero.
Worked example: Buddy goes 8 → 6 → 2 → -1. The final hunger is -1, 3 snacks were used, and the tipping snack was 3.
Real program
Read Buddy's hunger and exactly four snack values with the same fixed-count shape as Exercise 22. Keep the feed-then-check loop so its graded break is the only break.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 24: Feed Every Hungry Pet
Define a tiny Pet class whose __init__ stores only a hunger attribute. Make your own inline list named pets containing four Pet objects with hunger values 8, 3, 7, and 5, in that order. Use for pet in pets:. For each pet whose hunger is greater than 6, reduce that pet's hunger by 5 with long-form subtraction; leave pets with hunger 6 or below unchanged. Build and report a list of the resulting hunger values in the original order.
Worked example: The resulting hunger list is [3, 3, 2, 5].
Real program
Read hunger values with input() until a blank entry, convert each to int, construct the pets, and keep the conditional update and result-list report.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 25: Pet Talent Show
Define Pet with __init__(self, name, trick) that stores both attributes and a show_trick method that prints an f-string describing the pet performing its trick. Make a list of at least three pets with different tricks. Use one for loop to call each pet's show_trick method.
Real program
Read (name, trick) pairs into pets with the blank-name sentinel, then keep the loop that calls show_trick() for every pet.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Exercise 26: Pet-Care List
Define Pet with __init__(self, name) that stores the name and starts hunger at 5, plus a feed(self, amount) method that lowers hunger. Make a foods dictionary and a list of at least three pets. Feed each pet a food amount from the dictionary, then use one for pet in pets: loop to print each pet's name and hunger in an f-string.
Real program
Read (name, food-key) pairs with the blank-name sentinel. Use each key to feed that pet inside the read loop, then keep the final print loop.
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.