Lab 1: VSCode and Python
Section 1: Directory Navigation and VSCode
Before we start, make sure you setup VSCode using the class Setup Guide. If you haven’t done so, please follow the instructions in the Setup tab in the course website.
1.2 Understanding Directories and File Systems
Before we dive into using VSCode, it’s important to understand how files are organized on your computer:
- Directory (Folder): A container for files and other directories.
- File: A named collection of data.
- File System: The way your computer organizes and stores files.
Your file system is like a tree:
/
├── Users
│ └── YourUsername
│ ├── Documents
│ ├── Desktop
│ └── Downloads
├── Applications
└── System
Here, /
is the root directory. Users
, Applications
, and System
are directories. Documents
, Desktop
, and Downloads
are subdirectories of YourUsername
. To go to root, you can use /
or ~
to go to your home directory.
1.3 Basic Terminal Commands
The terminal (also called command line or console) allows you to interact with your computer using text commands. Here are some essential commands:
pwd
: Print Working Directory (shows your current location)ls
: List contents of the current directorycd
: Change Directorycd DirectoryName
: Move into a directorycd ..
: Move up one levelcd ~
: Move to your home directory
mkdir
: Make a new directorytouch
: Create a new filerm
: Remove a file (use with caution!)rm -r
: Remove a directory and its contents (use with extreme caution!)mv
: Move a file or directorycp
: Copy a file or directory
1.4 Using the Terminal in VSCode
- Open VSCode
- Access the integrated terminal:
- Use the menu: View > Terminal
- Or use the shortcut:
- Windows/Linux: Ctrl + `
- Mac: Cmd + `
Quick tip: To open VSCode from terminal, type code .
in the directory you want to open.
1.5 Creating a Project Directory
Let’s create a directory for your CS112 projects:
- Open the VSCode terminal
- Navigate to where you want to create your project folder:
$ cd Documents
- Create a new directory:
$ mkdir CS112
- Verify that the directory was created:
$ ls
- Move into this new directory:
$ cd CS112
Task: Create a new directory called CS112_Labs
within your CS112
directory.
1.6 Opening a Project in VSCode
- In VSCode, go to File > Open Folder
- Navigate to and select your CS112_Projects folder
- Click ‘Select Folder’
Now you’re set up to start coding in your new project directory!
Section 2: Inventory Access
Now that we’re familiar with navigating directories and creating Python files/environments in VSCode, let’s tackle a simple inventory access program.
This inventory access program allows the user to collect resources, view their inventory, and craft items with their resources. Take a minute to look at main to see how these interactions are executed by calling functions that we will complete. Additionally, look at the dictionaries inventory, world, and resources. Our program will be interacting with these data structures. Throughout this lab, you may find some dictionary/list methods and operations useful.
Part 1: Collect Resources
One of the fundamental functions of an inventory system is storing items, however before you can store a resource you need to collect it! In this program, our world consists of different biomes, which each have a different combination of resources. We can gather from a biome of choice, however the resource you obtain is random.
Task 1: Define the first of our methods: collect_resources
. This method should process the input biome, and if it is a valid biome in our world, randomly choose one of the resources in the input biome to add to our inventory. We’ve imported the random library, which allows us to use random.choice
. Lastly, your method should return a formatted string containing information about what resource was collected from the input biome.
Note: Navigate to the documentation for the random module to learn more about the random.choice
method.
Part 2: Display Inventory
In addition to updating the inventory, it’s convenient to be able to see what items you have in your inventory, as well as how many of each item you have.
Task 2: Define the method display_inventory
. This method should format and print the current status of your inventory, displaying the item and the quantity (AKA simply printing the inventory dictionary will not suffice!). Note that this method takes no arguments and does not return anything.
Part 3: Craft Item
Having resources is great, but putting them to use can be even better! If we combine our resources according to a given recipe, we can craft items.
Task 3: Define the method craft_item
. This method should process the input item, and if it is a valid craftable item, check whether or not the user has the required number of resources to craft the item. If so, the user should use these resources to craft the item, subtracting the resources and adding the item to the inventory. Similarly to collect_resources
, this method should return a formatted string containing information about the item crafted.
Section 3: You’re All Set!
Success! Congratulations on completing the first lab of cs0112! Please call over a TA to get checked off.