We want you to get set up with PyCharm, and we want to get some Python practice with functions, lists, and for loops in! As always, if you need help with any of these concepts, please come to TA hours!
testlight.py
into each project you create.return
Functions in Python look a little bit different than they do in Pyret. For instance, if we wanted to write a function that takes in a Number
representing the time of day (in military time) and output either "Day"
or "Night"
in Pyret, we'd do something like this:
In Python, we could write it like this:
Note that when we want our functions to give us an output when we call them in Python, we have to write return
before the value we'd like to output.
function_one
:In words, describe what this function returns for each of these inputs. In particular, when does it return False
? When does it return True
?
function_two
:What does this function do? How is it different than function_one
? Again, what does it return for these inputs:
function_three
:What does this function do? How is it different than function_one
and function_two
? What does it return for these inputs:
If you know how to solve any of these problems using techniques you've learned outside of 111, don't! Make sure to only use Python concepts we've talked about in class.
Create a new Python file called lab8_code.py
.
Remember your resources!
In Monsieur Gusteau's bakery, the staff are preparing for the holiday season. In order to optimize the time they spend in the kitchen, they want to know which dessert is their customers' favorite! They've decided to try and sort through the order slips from the past month to figure out which dessert is most popular.
For the following tasks, you can assume an order is just the name of a dessert (though capitalization might be inconsistent!)
Task: Write a function dessert_matches
that takes a order
(string) and a dessert
(string) and returns a boolean indicating whether the order is for the given dessert, regardless of how the order and dessert have been capitalized (that is, ignore case).
Note: You can lowercase a string s
in Python by writing s.lower()
(where s
gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operators and
, or
, and not
.
Task: Write a function any_orders_for
that takes a dessert
(string) and orders
(list of orders) and returns a boolean indicating whether any orders were for the given dessert (case insensitive).
Task: Write a function count_orders_for
that takes a dessert
(string) and a orders
, a list of orders, and returns the number of orders that were for the given dessert (case insensitive).
Task: Write a function record_order
that takes in orders
, a list of orders, and new_order
, a new order, and adds the order to the end of the list. This function should not return anything!
To do many of the following problems, you will need to do something called string slicing. String slicing is the Python equivalent of Pyret's string-substring
, but winds up being a lot more powerful.
Let's say you have the string str = "Beep!"
. Just like in Pyret, strings in Python are 0-indexed:
To access the characters at those indices, use the notation: str[index]
. For example,
If you want to slice a string into a substring of more than one character, use the notation: str[start:end]
where start
represents the first index of the substring (inclusive) and end
represents the last index of the substring (exclusive). For example,
That's all you need to know for this assignment, but Python has a few additional shortcuts for string slicing that might be useful in the long run:
If you leave off the start or end index, Python assumes you want to start at 0 and end at the end of the string:
Negative numbers let you index from the end of a string
Task: Write a function first_three_letters(dessert: str, orders: list)
that takes in a dessert name and all orders and returns a list of all orders for desserts with the same first three letters in its name as dessert
(regardless of capitalization). You can assume all strings in this function are at least three letters long.
Congratulations! You've gotten more practice with Python, and working at Gusteau's bakery has shown you that Anyone Can Cook!
Brown University CSCI 0111 (Fall 2020)
Do you have feedback? Fill out this form.