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.
We’ll go through a couple scenarios in which you might want to search online for information. For each scenario:
Then, after you’ve made a ranking for each scenario, think about the common elements of the good and bad queries.
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:
python 2+"1"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
python add int and string
python strings
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:
print(“hello”) but without new line
python print
python print without new line
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:
“[]” python
check if list is empty
python lists
python check if list is empty
python list length
After thinking about your own takeaways, take a look at the TAs’ recommendations here.
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.
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.
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.
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).
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:
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.
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:
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!