To start this assignment, download this zip file.
Lab 4b — Substrings
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab4b
.
Review the guide
4 minutes
Review the guide on strings, particularly replacing a substring, limited replacement, and checking for a substring.
Goals for today
Understand that
string.replace()
does not mutate the string.Understand how to use
in
to tell if a substring is part of a bigger string.Understand how to use
in
to check if an item is in a list.Understand why using
in
saves us from using a for loop.
Changeup
5 minutes
For sake of time, work through these exercises with the TA.
In the file changeup.py
, you will find a set of substring replacement
exercises. The main()
function calls a set of functions that change a proverb
into a new proverb:
print(change_excuse('A bad excuse is better than none.'))
print(change_mountain('Do not make a mountain out of a mole hill.'))
print(change_fire('Fight fire with fire.'))
print(change_late('Better late than never.'))
The function documentation shows you what changes to make. You should use
replace()
multiple times, replacing consecutive words. We have provided one
example for you:
def change_excuse(proverb: str) -> str:
"""
Change: A bad excuse is better than none.
To: Every good excuse is worth millions.
"""
proverb = proverb.replace('A', 'Every')
proverb = proverb.replace('bad', 'good')
proverb = proverb.replace('better than', 'worth')
proverb = proverb.replace('none', 'millions')
return proverb
This does calls replace()
four times to make the change.
At the end, you will find a place to change your own proverb:
# https://en.wikipedia.org/wiki/List_of_proverbial_phrases
print(change_your_own('Put a phrase here'))
You can use the Wikipedia list of proverbs for examples.
Discussion with TA:
- Were any of these tricky?
- Did you need to use limited replacement at all?
- Why is reassigning the variable
proverb
important each time we callreplace()
?
Note: .replace()
does not mutate, or change strings. It simply returns a new string. To save the new string we write string = string.replace(old, new)
.
Simple chat bot
10 minutes with a partner, 5 minute review
Write a simple chatbot using the code in chatbot.py
. The chatbot follows these
rules:
- start with, “Hello! What did you do today?”
- read input until “goodbye”
- if input has ‘good’, ‘great’, ‘fun’, or ‘amazing’
- response is “Cool!”
- if input has ‘bad’, ‘rough’, ‘sad’, or ‘terrible’
- response is “I’m so sorry, how can I help?”
- otherwise
- response is “Tell me more.”
We have decomposed this problem into two functions:
-
found_word(text, words)
— This takes a string intext
and a list of strings inwords
. The function returnsTrue
if any of the words in the list are found in the text. You can usein
and early return in this function. -
chat()
— This function uses an input loop to implement a chat bot, using the rules listed above.
Discussion with TA:
- What does early return mean?
- What happens if a person types something that contains a word from both lists?
Assigning jerseys
5 minute overview, 10 minutes with a partner, 5 minute review
Write a program that signs up players for a soccer team. The program asks you to input a player’s name and jersey number. If a jersey number is already taken, then it tells you that number is already taken and asks you to try a different number. You end the list of players by entering a blank name for a player.
Once the program has all the players, it prints out a list of players and their jersey numbers.
Here is some sample input and output:
Player name: John
Jersey number: 10
Player name: Peter
Jersey number: 10
That number is already taken.
Jersey number: 5
Player name: Sarah
Jersey number: 1
Player name: Jane
Jersey number: 10
That number is already taken.
Jersey number: 5
That number is already taken.
Jersey number: 1
That number is already taken.
Jersey number: 3
Player name:
10: John
5: Peter
1: Sarah
3: Jane
We have decomposed this problem into three functions:
-
get_player_info(jerseys)
: This function gets a player’s info and returns a tuple of(name, number)
. The function is given a list of jersey numbers that are already taken. The function should returnNone
if the name is empty. The function should keep asking for a number until the player chooses one that is not already taken. -
assign_jerseys()
: This function gets all of the players and returns a list of(name, number)
tuples. It usesget_player_info()
to get each player. -
print_assignments()
: This function prints all of the players and their numbers using the formatnumber: name
Note, when you call get_player_info()
you need to give it a list of all of the
numbers that have already been assigned. To do this, you will need to unpack the
tuple returned by get_player_info()
. You can do this with:
name, number = player
Discussion with TA:
-
The
get_player_info()
function has a little twist compared to the past funcitons we have written like this, because you have to keep asking for a player’s number until you get one that is not assigned. Show a solution and talk through it to be sure everyone understands how this works. -
Likewise, the
assign_players()
function does something different than usual — we need a list of players and a list of jerseys. Show a solution and talk through it to be sure everyone understands how this works.
Grading
To finish this lab and receive a grade, take the canvas quiz.
Solution
We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)