CS2 Lab 5 2024, Python I 🎰

<<< Back to main site

Due Date: Thursday, November 14, 2024
Complete this lab before Lab 6 (Python II) and Project 5.

Need help? Remember to check out Edstem and our Website for TA help assistance.

Background 🎰

Be sure to read all background notes, as they are vital to do the assignment.


Project/Lab Description 🎰

Lab Goals:

Downloading Python

The final part of the course will be using Python. If you do not have Python 3 installed, you can follow the instructions on this website to download the correct version for your device. If you believe you have already downloaded Python, you can check which version of Python you have installed by running python3 --version in your terminal / command prompt.

Download the VSCode Python Extension

In addition to downloading the software, please download the Python extension on VSCode as well. Follow the steps below to download the extension:

  1. Go to the “Extensions” tab on VSCode (located on the lefthand side of the IDE).
  2. Search “Python” in the 'Search Extensions in MarketPlace'. You’ll see three options popping up at the top.
  3. Select the one that includes the word “Pylance” in the description, and install it (the first option in the following screenshot).

Once you have that extension installed, you should be set to code in VSCode! When writing in Python, remember to have your file end in .py (example.py).


Tasks 🎰

Task 1 Double click your CS2 folder on your desktop, and enter your folder for Python. Then, create a new folder inside called Python Lab I. All your files for the lab should live in this folder.

Task 2 Download the stencil file for this lab, and put it in your Python Lab I folder you just created!

Task 3 Open up your Python Lab I folder by dragging the folder into Visual Studio Code, or by opening up Visual Studio Code, clicking Open under the Start header, and finding the folder in your file system.

Once the folder has opened, you should see our stencil file, CS2_2023_Lab5_Stencil.py on the lefthand side of the screen. Double click the file, and you should see that it already contains some lines of code. Now we'll learn how to run that code!

Task 4 In the file provided, we've written a line of code containing a print statement. Run the code by clicking the play button on the top right corner of the screen to see what happens.

This program uses the print method to print the string "hello, world!" in the terminal. The terminal should appear at the bottom of your VSCode window once you click run. Methods (also called functions) are blocks of organized, reusable code that perform a single action. You’ll write your own function later!

Math Operations Practices

In Python, text between quotation marks is known as a string. For example, 6 is an integer, but "6" is a string.

Here is a list of some basic math operations in Python. More information about math operations in Python can be found here.

Task 5a Try running the following code by copy-pasting this line into line 5 of your stencil file.

print(20+4)

Task 5b The code block on line 6 contains a python comment. Prepending a line with # produces a comment. Try removing the comment and printing out your own math equation, as above.

Concatenation Practice

The + operator can be used for addition, as above, but can also be used with strings. This operation is called concatenation.

Task 6 Try running the following line of code:

print("hello, " + "world" + "!")

Because the + operator has different roles when used with numbers versus strings, it's important to always make sure you're only using it with just numbers or just strings, not both!

IMPORTANT NOTE: Please make sure that your quotation marks look straight (like in the code block above). If they appear angled or curly, delete them and retype them within VSCode.

Variables Practice

In Python, you can store data in variables. More information about variables in Python can be found here.

Task 7a Try running the code below. In this variables practice section, it is important to run all of the code blocks in order, as some depend on the ones above.

width = 16
print(width)

Task 7b Variables can also be used with other variables. To see this, use your stencil file to

  1. Create your own variable called height and assign it the value 7
  2. Print this new variable
  3. Create a new variable area and assign it the value of width multiplied by height using the two variables above
  4. Print this variable area

List Practice

In Python you can make lists to group things together. More information about lists in Python can be found here.

casino_games = ["blackjack", "roulette", "poker"]
print(casino_games)

The items inside of the square brackets can be any combination of strings, numbers, or other lists. Each item in your list must be separated by a comma.

Each item in a list has an associated list number, or index. List indexing starts at zero. In casino_games, "blackjack" is at index zero, "roulette" is at index 1, and "poker" is at index 2. This is called zero indexing.

To access items in your list, you use bracket syntax, like so:

my_list[index_number]

where my_list is the name of your list and index_number is an integer and the index of the item you’re trying to access.

Task 8 In your stencil file, write code to print the second index/third item in casino_games.

Task 9 We can alter items in the list in a similar manner. Try running the code below.

casino_games[0] = "slots"
print(casino_games)

To add an item to the end of your list you can use the append function as so:

casino_games.append("baccarat")
print(casino_games)

Try adding your own casino game to the end of the list in your stencil file!

Conditionals Practice

Conditional expressions use logic to determine which path of action to take based upon conditions set by the programmer. You used them in JavaScript and they are pretty similar in Python, with slightly different syntax. More information about conditionals in Python can be found here.

Conditional statements are often set up in the following way:

if condition1:
    do something
elif condition2:
    do something else
else:
    do something else

Note that "elif" is short for "else if." Only the first if block is necessary in conditional statements; there can be none, one, or multiple elif blocks after an if block, and there can at most be one else block following an if block. This depends on what you’re trying to achieve in your code.

Task 10 Try running the following code with different input to see what happens.

number = input("Type in 1 or 2")

if number == "1":
  print("nice choice!")
elif number == "2":
  print("cool!")
else:
  print("that's not 1 or 2!")

Some important things to remember for conditional statements:

Alignment is VERY important in Python! Make sure that you follow the alignment (proper indentation) seen throughout the lab.

The following is a list of conditional operators:

A brief note about and, or, not:

Task 11 Try running the following examples:

if 8 > 5:
  print("8 is greater than 5")
else:
  print("8 is not greater than 5")

if 10 <= 6:
  print("10 is less than or equal to 6")
else:
  print("10 is greater than 6")

Task 12 Try editing the variable values below so that the code prints "I love blackjack!":

# edit this code!
skill_based = True
player_to_player = True

# do not edit this code!
if (skill_based == True) and (player_to_player == True):
  print("I like poker!")
elif not(skill_based == True) and not(player_to_player == True):
  print("I like slots!")
else:
  print("I love blackjack!")

Putting It All Together :)

Task 13
Zach has recruited 12 friends to shuffle decks of cards. Each friend can shuffle 4 decks per hour.

In the stencil file, write code to solve this problem:

a variable num_friends with value 12
a variable decks_per_hour with value 4
a variable total_time that stores how long it will take (in hours) for Zach's friends to shuffle 240 decks.

If you're having trouble, look back at Math Operations and Variables Practice!

Task 14
In the stencil file, add onto the code below in the following manner:

  1. Append another name to the end of the list.
  2. Use list indexing to print out "clubs".
  3. Then replace "clubs" with a different suit.
  4. Print the element at that same index to show the change.

Optional: figure out how to remove items if you disagree with our list and print the ammended list.
If you're having trouble, look back to Lists Practice!

suits = ["clubs", "hearts", "diamonds"] # do not edit this line!

# Your code here

Task 15
In the stencil file, create a program that checks for how many cards a player has, from 1 to 52.

To do this, we must first assign the initial number of cards in the store. Write a value called total_cards and assign it a value from 1 to 52.

Now, we will write some conditional statements to print out a message based on the value of total_cards. Based on the availabe number of cards, several things can happen:

If you're having trouble, look back to Conditionals Practice!


Hand-In 🎰

To Hand-In Lab 5:
Come to hours and show your TA two things for checkoff:

  1. The downloaded Python and extension on VSCode.
  2. Output for Tasks 13-15.

Congrats! You just finished your second-to-last CS2 lab! 🎰

If you have any issues with completing this assignment, please reach out to the course HTAs: cs0020headtas@lists.brown.edu

If you need to request an extension, contact Professor Stanford directly: don.stanford@gmail.com