Computer Science

# Lab 7 — Strings and Doctests

In this lab, you will write functions that operate on strings. For each function, you will write doctests.

Create a file called `strings_and_tests.py` and you will use this file for all the problems in this lab.

## Exclaim

Juliette loves to type text messages with lots of punctuation. She’s very enthusiastic, so she’ll often repeat the exclamation a bunch… Here’s one of her recent text conversations:

Juliette: “Did you see Amanda Gorman at the inauguration?!?!?!?!” “Yes, her poem was great. Did you like it?” Juliette: “It was amazing!!!” “Agreed! And she is only 22. What will you have accomplished by the age of 22?” Juliette: ”…”

Juliette is getting tired of repeating the punctuation so much. Help her out by writing a Python function that does it for her! The function is `exclaim(message, end, repeat)`:

• `message`: a string
• `end`: a string, the ending punctuation
• `repeat`: a number indicating the number of times to repeat the punctuation at the end of the message

The function should return the resulting message.

For example:

``````result = exclaim('Did you see Amanda Gorman at the inauguration', '?!', 6)
print(result)
> Did you see Amanda Gorman at the inauguration?!?!?!?!?!?!

result = exclaim('CS 110 is awesome','!', 5)
print(result)
> CS 110 is awesome!!!!!``````
1. Write a docstring for this function
2. Write a doctest for this function
3. Write the function and test it
4. In the main body of the file, call your function 1 time and print the results to the screen.

Remember that you can type triple double quotes `"""` in PyCharm, and then `Return` and Pycharm will fill in a template for the Docstring.

When writing doctests that return strings, use single quotes and not double quotes.

## Spacing

Write a function `spacing(word, number_of_spaces)` that puts spaces between every letter of a word. The parameters are:

• `word`: a string
• `number_of_spaces`: an integer, representing the number of spaces to adding

The return value is a string with spaces between all the characters. For example:

wordnumber_of_spacesreturn value
hello3h   l   l   o
why2w  h  y
melon0melon

If the `number_of_spaces` is less than zero, then treat it the same as if it was zero.

Call your `spacing()` function at least 5 times and print the results to the screen.

1. Write a docstring for this function
2. Write a doctest for this function
3. Write the function and test it
4. In the main body of the file, call your function 1 time and print the results to the screen.

## Word Guess

Use the file `word_guess.py` to write your code for this problem.

Write a function, `word_guess(guessed_word, secret_word, guess)` that helps us play a word guessing game. The function takes in 3 paramters:

• `guessed_word`: a string representing what we’ve guessed so far in the word; unguessed letters represented by ”-”

• `secret_word`: a string representing the word we are trying to guess

• `guess`: a string representing the letter we are guessing this round

The function should return a string that represents our guessed word updated by the most recent guess.

Remember that strings are immutable, so the function needs to create a new string letter by letter.

Here are some examples of input and output

guessed_wordsecret_wordguessreturn value
------Pythono----o-
----o-Pythone----o-
----o-Pythony-y—o-
------mooingo-oo---
1. Write a docstring for this function
2. Write a doctest for this function
3. Write the function and test it
4. In the main body of the file, call your function 1 time and print the results to the screen.

## Guessing game

This is an optional activity! Download guessing-game.zip. Take your code for the `word_guess()` function and put it inside the `guessing_game.py` file. See the directions at the top of `guessing_game.py`. Enjoy!

Don’t worry about understanding all the other code in `guessing_game.py`. But by the end of the course, you should be able to understand all this!

## Lessons

What we want you to get from this lab:

• You understand how to use strings

• You can write functions that loop over strings and return strings

• You can clearly document your functions with docstrings

• You can write doctests and use them to test your functions

• You can figure out what went wrong when something unexpected happens