To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Lab 3a — Interactive programs
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 lab3a
.
10 minutes
In your lab3a
folder, find the file called ozymandias.py
.
Experiment with printing the lines of the poem
Ozymandias by Percy Bysshe Shelley :
I met a traveller from an antique land,
Who said—“Two vast and trunkless legs of stone
Stand in the desert… . Near them, on the sand,
Half sunk a shattered visage lies, whose frown,
And wrinkled lip, and sneer of cold command,
Tell that its sculptor well those passions read
Which yet survive, stamped on these lifeless things,
The hand that mocked them, and the heart that fed;
And on the pedestal, these words appear:
My name is Ozymandias, King of Kings;
Look on my Works, ye Mighty, and despair!
Nothing beside remains. Round the decay
Of that colossal Wreck, boundless and bare
The lone and level sands stretch far away.”
Notes about strings and quotation marks:
-
When typing quotations, they may contain single quotation marks
'
, double quotation marks"
, or stylized quotation marks“
. -
When using Python strings, you can use either single quotation marks, such as
'I meet a traveller'
or double quotation marks, such as"I met a traveller"
.
Quotation marks:
Try printing a string that has these words or characters.
- How can you include the word
don't
inside a string? - How could you include
Who said—"Two vast
inside your string? - What happens if you put a backslash
\
before a quotation mark? - How could you use a backslash to include both
'
and"
in a string? - Why is this stylized quotation mark
“
special?
Printing multiple lines:
- How can you use multiple
print()
statements to print on multiple lines? - What happens when you put
\n
in the middle of a string?
You can use triple-quotes to define a multi-line string.
def run():
print("""
First line
This shows in the second line.
Unfortunately, all of these lines print indented.""")
A great way to print multiple lines is like this:
def run():
print("First line\n"
"This shows in the second line.\n"
"This prints how you expect, and looks nice.\n")
Formatted strings
5 minutes
In your lab3a
folder, find a file called formatting.py
, which has this code
in it:
def main():
noun = 'dog'
verb1 = 'jumped'
verb2 = 'danced'
adverb = 'gracefully'
noun2 = 'fish stick'
# Write code here
if __name__ == '__main__':
main()
Write code at the end of main()
that uses a formatted string to print out:
The dog jumped and danced gracefully, so he won first prize -- a fish stick.
Remember, that a formatted string starts with f
, before the quotation marks.
Inside the quotation marks you use curly brackets {}
for variables:
print(f'This is a {adjective} experience.')
- Ask the TA questions about formatted strings
Debugging practice
15 minutes
Open the file file called wendys.py
.
def get_name() -> str:
return input("What is your name? ")
def get_sandwich() -> str:
return input("What kind of sandwich do you want? ")
def get_additions() -> str:
return input("What do you want on it? ")
def print_summary(name: str, sandwich: str, adds: str):
print(f"{name} wants a {sandwich} sandwich with {adds}!")
def main():
print("Welcome to Wendy's!")
name = get_name()
sandwich = get_sandwich()
adds = get_additions()
print_summary(name, sandwich, adds)
if __name__ == '__main__':
main()
Put a breakpoint on the first line of main()
, the line that uses print()
:
def main():
print("Welcome to Wendy's!")
name = get_name()
sandwich = get_sandwich()
adds = get_additions()
print_summary(name, sandwich, adds)
The TA will demonstrate each of these steps and you should be sure you can do and see the same things on your computer:
-
Run the program normally and show how it works.
-
Note that the program does not stop at the breakpoint.
-
Start the debugger.
-
Drag the console window to be next to the debugger window.
-
Use step over to step over every line of main. Examine all of the variable values and return values.
-
Restart the debugger and use step into my code to step into each function and execute every line of code.
-
Set a breakpoint at the start of every function. Then restart the debugger and use resume followed by step over to jump to each of the breakpoints, execute one line of code, and then go to the next breakpoint.
-
Change the
get_name()
function so it looks like this:
def get_name() -> str:
entered_name = input("What is your name? ")
return entered_name
Run the debugger and examine the value of entered_name
.
This code does the same thing as the original:
def get_name() -> str:
return input("What is your name? ")
It just takes one extra step to first store what the user enters in a variable and then return it. Ask the TA any questions you have about the differences between these.
Testing code with pytest
15 minutes
For upcoming homeworks and projects we will provide you with tests using a
library called pytest
. Testing your code is how you ensure it does what you
expect in a variety of situations.
For this problem, the TA will walk you through the
guide on using pytest. Use the wendys.py
and
test_wendys.py
that are in your lab3a
folder. You don’t need to download
the zip file from the guide.
Do the following in the guide:
-
Install
byu_pytest_utils
by following the instructions. You should have already done this at the beginning of the semester. -
Run the pytests for
wendys.py
. -
Break
wendys.py
by following the guide. Show what happens, then fix the code and re-run the tests. -
Break
wendys.py
again by removing a space at the end of aninput()
prompt. Show how to find this problem when running the test. -
Be sure to ask any questions you have about pytest.
Grading
To finish this lab and receive a grade, take the canvas quiz.
We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊