# 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:#
Function Definition: Define a function
analyze_tile(message, location="Unknown")
.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. """
Input Handling:
message
(required): A string containing the tileโs message.location
(optional, default"Unknown"
): The place where the tile was found.
Error Handling:
Raise a
ValueError
ifmessage
is an empty string.Raise a
TypeError
ifmessage
is not a string.
Return Statement:
If valid, return a formatted string:
Tile Found: <message>
Location: <location>
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")
orraise 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")