BYU logo Computer Science

To start this assignment, download this zip file.

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.

Print

5 minutes

In your lab3a folder, find the file called ozymandias.py. In this file, write a program that prints out 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".

  • If you want to include a single quotation mark in the string, you should use double quotation marks to surround the string:

print("I haven't seen him")
  • If you want to include a double quotation mark in the string, you should use single quotation marks to surround the string:
print('He said, "I have not seen him."')
  • If you need to use both, then you can escape a quotation mark with a backquote \:
print('He said, "I haven\'t seen him."')

This poem uses stylized quotation marks, so you shouldn’t have any problems regardless. :-)

Formatted strings

10 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:

  1. Run the program normally and show how it works.

  2. Start the debugger.

  3. Drag the console window to be next to the debugger window.

  4. Use step over to step over every line of main. Examine all of the variable values and return values.

  5. Restart the debugger and use step into to step into each function and execute every line of code.

  6. 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.

  7. 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:

  1. Install byu_pytest_utils by following the instructions.

  2. Run the pytests for wendys.py.

  3. Break wendys.py by following the guide. Show what happens, then fix the code and re-run the tests.

  4. Break wendys.py again by removing a space at the end of an input() prompt. Show how to find this problem when running the test.

  5. Be sure to ask any questions you have about pytest.

Grading

To finish this lab and receive a grade, take the canvas quiz.