Tutorial Table of Contents:
Introduction and demonstration – 0:00
Define the pseudocode – 2:00
Initialize the pet – 4:10
Main game loop and the menu – 11:15
Quitting the simulator – 16:40
Executing other functions – 20:10
Adding functionality – 22:30
playToys
– 26:40feedPet
– 27:40getToys
– 28:30Final touch-ups – 33:50
Extending the project & Conclusion – 34:58
- Please note that there's a typo in line 24 in the video, where
toyNum > len(petToys)
should betoyNum >= len(petToys)
!
Project Overview
In this project, Juni instructor Gabriel will be showing us how use Python to build a Pet Simulator! In the simulator, you can name, feed, and play with your very own virtual pet.
Learning outcomes:
This is a relatively longer project great for practicing parameter passing and their associate scopes, functions, loops, and variables.
Language: Python
Juni Level: Python Level 2
Core concepts: Functions, Loops, Taking input, Interactivity, Input validation
Lines of Code: ~200
Difficulty: Moderate/Hard
Est. Time: 1.5+ hr, but this varies by student and how much you add to the simulator!
Important Concepts to Review or Learn Beforehand
Pseudocode - Logical, written steps of our program written in plain language - not code. This defines what our program will need to be comprised of in order to make everything work together.
Integer - This is a type of data that keeps whole numbers – such as 1, 0, -10, 10923.
String - This is a type of data that stores characters – such as letters from the alphabet, numbers, and symbols. For example, “Hello world!” or “I read 3000 books!”
List - A data structure that keeps its items in sequential order – for example, [“Alex”, “Ben”, “Taylor”] may indicate that Alex is first, Ben is second, and Taylor is third. Lists also are indexed at 0 – that means that you can access the first value of a list by using [0]. For example, if the above list had the name “names”, you could access the “Alex” value by using
names[0]
.User input - To make the program pause and wait for input from the user, and use that input for future steps of the program.
Input validation - To validate the inputs that the user gives us before trying to execute commands that rely on user input. By “validate”, this typically means that we check the user’s input against a set of possible values that we have.
Parameter passing - This is where we pass different variables, values, or data structures.
Project Instructions:
Create a simulator where you can “raise a pet.” This will involve looping through a menu, giving options to feed, and buying different things at a shop (e.g. pet bowl, fish feed, etc. based on what kind of pet that the user inputs).
You will utilize parameter passing, functions, loops, dictionaries, and variables.
Bonus: Challenge Yourself
Extend the project by adding more functions and actions that you can think of, and adding them to the menu.
Try adding another type of pet, with its own toys.
Add new properties to the basic pet dictionary that interact with new functions.
Try to do the project on your own first, and get creative with it if you want! Gabriel has provided some guidelines and tips below to help you get started.
If you get stuck or want to check any part of your code, check out Gabriel’s solution and tutorial video at the bottom of this post, where he explains his thought process and how to build each part of his simulator.
Function Breakdown
We will break down the problem into its composite steps, making them functions as much as possible. Throughout this project, we will typically implement some kind of data validation to ensure that what the user input is actually a valid input before attempting to use it.
How can we store data for our project?
Pet Information - This could be stored inside of a dictionary, which would allow for easy access of its different properties.
Pet-type Specific Toys - This can similarly be expressed as a dictionary, where the keys are the different pet types. This would then double as a list of the different pet types that we are allowing in our application.
Menu options - We can store different keys to menu items by using them as the keys. We can further have a dictionary for each key that stores the text we want to be printed in the menu, and also stores a reference to the function corresponding to the key. For example, quit may be stored as
{“Q”: { “function”: quitSimulation, “text”: “Quit the simulation”} }
. We could then callmenuOptions[“Q”][“function”]()
to invoke thequitSimulation
function. We could also use the“text”
field to print out information to the user in the menu. By having these keys, we can therefore select different menu options by just using the key we choose rather than writing out an entire command.
What functions do we need in our menu?
quit
- This should handle when the user wants to quit from the main program.getToy
- Gets a new toy specific to the type of pet that is selected, by creating a pet toy dictionary. We can write out the list of potential toys that are specific to this pet, and then prompt the user for which one they would like.playToys
- Prints out some information where the user is able to “play” with the pet. This also modifies some parameters of the pet, e.g. “playfulness” if desired.feedPet
-This function decreases the hunger level of the pet.
What general functions do we need?
initPet
- This is meant to initialize the pet with some basic parameters such as type and name. Similarly, we should validate the input here to ensure that the input the user gives us is a part of the pet types that we allow.printMenu
- This function should iterate through our menuOptions dictionary and print the keys and their corresponding text.printStats
- This function should handle printing out some statistics about the current state of the cat: e.g. how hungry they are, what toys they have, etc.main
- This should be the section in which we control the loop of prompting the user what action they would like to take next. This should keep on looping as long as the menu selection is not Q. This should also increment the age property of our pet, as well as any other properties desired to be incremented.
Recommended Order of Construction
Proceed in building up what pieces make the most sense to you first before moving on. This could look like making a basic version of the menu and printing through the menu, or showing how different types of pet toys may work before moving on. Then, you can interlink these pieces before building out full functionality.
Project Solution & Tutorial Video
Check your answers or get help by viewing Gabriel's solution and step-by-step video tutorial above. Gabriel decided to add multiple pets to his simulator, and explains how to implement code for adding multiple options.
Want to keep learning?
We hope you enjoyed Gabriel's project tutorial!To keep practicing and learning, please check out all of our coding tutorials on our blog.
Need help?
Looking up your coding questions is one of the best ways to learn!
Another great way to learn is from an experienced coder or instructor. Juni CS instructors like Gabriel work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.
Read more about our coding courses and curriculum, or speak with a Juni Advisor today by calling (650) 263-4306 or emailing adivsors@learnwithjuni.com to learn which course is best for your child’s coding journey.