# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment

responses = initialize_assignment("4_toybee_tiles", "week_5", "lecture-not-graded", assignment_points = 10.0, assignment_tag = 'week5-lecture-not-graded')

# Initialize Otter
import otter
grader = otter.Notebook("4_toybee_tiles.ipynb")

๐Ÿ’ป Activity: Toynbee Tiles: Error Handling, Function Definitions, and Returns#

Question 1 (Points: 10.0): Unraveling the Mystery of Toynbee Tiles with Python! ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ”#

Instructions:#

The Toynbee Tiles are mysterious plaques embedded in urban streets, often containing cryptic messages. Your task is to write a function, analyze_tile(), that processes a tileโ€™s message while handling various errors.

Requirements:#

  1. Function Definition: Define a function analyze_tile(message, location="Unknown").

  2. Documentation: Include a docstring with the functionโ€™s name, parameters, and a brief description.

    """
    Processes a Toynbee Tile message while handling errors.
    
    Args:
        message (str): The message on the tile.
        location (str, optional): The tile's location. Defaults to "Unknown".
    
    Returns:
        str: A formatted string describing the tile.
    """
    
  3. Input Handling:

  • message (required): A string containing the tileโ€™s message.

  • location (optional, default "Unknown"): The place where the tile was found.

  1. Error Handling:

  • Raise a ValueError if message is an empty string.

  • Raise a TypeError if message is not a string.

  1. Return Statement:

  • If valid, return a formatted string:

Tile Found: <message>
Location: <location>
  1. Location Handling: If location is not a string, return an error message "Invalid location format.".

Example Usage:#

analyze_tile("Resurrect dead on planet Jupiter", "Philadelphia")

Expected Output:

Tile Found: Resurrect dead on planet Jupiter
Location: Philadelphia

If an error occurs, return a meaningful error message instead of crashing.

Helpful Information:#

  • Use if not message: to check for an empty string.

  • Use isinstance(variable, str) to check if a value is a string.

  • Use raise ValueError("message") or raise TypeError("message") for errors.

  • Ensure the function gracefully handles incorrect inputs.

# Function Definition
...
    """
    ...
    """
    # Error Handling
    # When message is not a string, raise TypeError, "Message must be a string."
    ...

    # When message is empty, raise ValueError, "Message cannot be empty."
    ...

    # When location is not a string, return "Invalid location format."
    ...

    # Return formatted output
    ...
    
# Example usage
print(analyze_tile("Resurrect dead on planet Jupiter", "Philadelphia"))
grader.check("toybee-tiles-function")

Submitting Assignment#

Please run the following block of code using shift + enter to submit your assignment, you should see your score.

from pykubegrader.submit.submit_assignment import submit_assignment

submit_assignment("week5-lecture-not-graded", "4_toybee_tiles")