To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
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 string
into a different string:
print(make_positive('That movie was bad, my popcorn tasted bad, and the weather is bad!'))
print(change_animal('Cats are so much fun! Cats are also so cute!'))
print(change_tense('I was on time, I was here, and I already went to school!'))
print(change_fruits('My favorite desserts are apple pie, strawberry cake, and strawberry ice-cream!'))
print(change_your_own('Put a phrase here'))
The function documentation shows you what changes to make. You should use
replace()
. We have provided one
example for you:
def make_positive(statement: str) -> str:
"""
Change: That movie was bad, my popcorn tasted bad, and the weather is bad!
To: That movie was great, my popcorn tasted great, and the weather is great!
"""
statement = statement.replace("bad", "great")
return statement
This code uses replace()
to change 3 different substrings.
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. 😊