Lab 1: VSCode and Python

View presentation

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:

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:

1.4 Using the Terminal in VSCode

  1. Open VSCode
  2. 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:

  1. Open the VSCode terminal
  2. Navigate to where you want to create your project folder:
$ cd Documents
  1. Create a new directory:
$ mkdir CS112
  1. Verify that the directory was created:
$ ls
  1. 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

  1. In VSCode, go to File > Open Folder
  2. Navigate to and select your CS112_Projects folder
  3. 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.