Lab 10: Using resources (Google and Stack Overflow)

Beep and Boop are ambitious little fellas, and even though they have now enrolled in and taken a bunch of different classes, they still have a lot of unanswered questions about things that weren’t covered in their courses. They have been directed to the internet – but the vast and juicy interwebs can be convoluted and overwhelming. Since they’re not yet accustomed to the ways of the world wide web, they’re going to need some help.

Beep and Boop often find themselves wanting to use a bit of code to handle a problem that’s not covered in the course. To do this, it helps to know how to search effectively online for operations and information on programming languages. This week’s lab is designed to help you, Beep, and Boop learn how to do this! (In other words, getting stuck and unstuck is part of the point this week, so don’t get frustrated.)

We will be working on searching online for answers to Python/coding questions! We’re going to start with a couple examples of scenarios and potential queries, and then we’ll apply these skills to write a program that reads from and writes to a file.

Googling for Python questions

We’ll go through a couple scenarios in which you might want to search online for information. For each scenario:

  1. Predict which example queries might be good and which might be bad
  2. Google the queries, and look into a couple of the search results. Try to note whether the same sites are consistently helpful.
  3. Write a ranking of the queries.

Then, after you’ve made a ranking for each scenario, think about the common elements of the good and bad queries.

Scenario #1: Getting Help on Error Messages

Beep and Boop are taking their first CS class, and they write the following code 2+"1", which throws an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'. Help Beep and Boop search online for help understanding the error.

Example queries:

Scenario #2: Getting Details on an Operation

Beep and Boop have grown quite fond of ASCII art, and they want to be able to use print, but without a new line (as is printed by default). In other words, they want to print("(\") and print("(\") to print (\(\ rather than:
(\
(\

Example queries:

Scenario #3: Finding an Appropriate Operation

Beep and Boop are doing list operations but want to write a base case that checks if a list is empty, but they don’t remember or know how to do that.

Example queries:

After thinking about your own takeaways, take a look at the TAs’ recommendations here.

Scenario #4: Which resources should I use?

Each time Beep and Boop do a Google search, they get just a little bit frightened at the pages and pages of results that pop up – how do they know which ones to click on? Which ones are actually legitimate and useful?

Let’s say you want to write a function that takes in two lists and adds them together elementwise. For example:

list1 = [1, 2, 3, 4, 5]
list2 = [10, 25, 7, 1, 1]

>> element_wise_add(list1, list2)
[11, 27, 10, 5, 6]

Consider the following three search results for the query: add elements pairwise across two lists Python

Discuss with your partner: which site was the most helpful? When might a Stack Overflow post be more useful? How about a blog post?

When you’re done, take a look at our list of suggestions.

File I/O

Beep and Boop are now equipped to navigate and effectively use sites such as Stack Overflow! They want to test their skills by tackling this topic that they’ve been hearing about a lot: File Input and Output in Python.

Beep and Boop learn that when you create a file on your computer, you can write a Python program that reads the contents of the file, makes calculations, and even writes data to a new file on your machine.

Candy 2.0

Beep and Boop have recently been craving candy a lot, so we’ve been requested to revisit the candy dataset from Lab 3.

Unlike Pyret, Python has no built-in table functionality (reading a table directly from google sheets, table functions, etc). To complete this lab, we’re going to have to take advantage of Python’s ability to mutate data, iterate through data, and read and write data to and from input/output files.

Setup

  1. Start off by creating a directory in your computer called ‘lab10’. This directory should be in the same place as your PycharmProjects folder.
  2. Navigate to this GitHub file. In the top right, click ‘Raw’. You should see a wall of text on your browser.
  3. Go ahead and copy the entire page of text, paste it into a simple text editor (something like TextEdit for Mac or Notepad for Windows. Call over a TA if you’re not sure what to use), and save the file as ‘candy-data.csv’ inside your ‘lab10’ directory.
  4. Create a Python file called ‘candy-data.py’ inside your ‘lab10’ directory. This is where you’ll implement the functions below.

Your assignment

You, Beep, and Boop should be experts on surfing the web for relevant information and answers now, so let’s put those skills to the test. We aren’t going to give you much guidance about how to complete these tasks; remember the takeaways from Part 1, and try to use online resources (but if you get stuck, the TAs are still here to help).

  1. Write a function that reads from ‘candy-data.csv’ and calculates the name of the candy with the highest win percentage.’

    HINT: If you’re not sure where to start, try following the steps below:

    • Figure out how to read a csv file into Python. Try to find a method that turns the csv into a datatype you’re already familiar with.
    • Try to print out a few win percentages for different candies to make sure you know how to access them.
    • Design your function! Calculating the highest win percentage shouldn’t require looking up new approaches. Instead, use things that we’ve already learned in class (for loops might be helpful here)
    • Test your function to ensure that it works properly. Call over a TA when you’re done.
  2. Write a second function that writes the results of your answer to Question 1 to a file named ‘result-1.csv’.

    HINT: This time, start by writing a function that just prints the string “Hello, World” onto the first line of a file. Once you’ve done so, integrate data from the CSV.

  3. Write a function that reads from ‘candy-data.csv’ and writes the names of the candies with chocolate to a file named ‘result-2.csv’, such that each name is on a separate line.

    HINT: Consider which parts of the code your wrote for problems 1 and 2 help you solve this problem.

NOTES:

Graduation

Now that Beep and Boop have learned how to find everything they need on the internet, they’ve fullfilled all 30 credits at Brown and are graduating with full honors!