1. Learn
  2. /
  3. Courses
  4. /
  5. Writing Functions in Python

Connected

Exercise

HTML Generator

You are writing a script that generates HTML for a webpage on the fly. So far, you have written two decorators that will add bold or italics tags to any function that returns a string. You notice, however, that these two decorators look very similar. Instead of writing a bunch of other similar looking decorators, you want to create one decorator, html(), that can take any pair of opening and closing tags.

def bold(func):
  @wraps(func)
  def wrapper(*args, **kwargs):
    msg = func(*args, **kwargs)
    return '<b>{}</b>'.format(msg)
  return wrapper
def italics(func):
  @wraps(func)
  def wrapper(*args, **kwargs):
    msg = func(*args, **kwargs)
    return '<i>{}</i>'.format(msg)
  return wrapper

Instructions 1/4

undefined XP
  • 1

    Return the decorator and the decorated function from the correct places in the new html() decorator.

  • 2

    Use the html() decorator to wrap the return value of hello() in the strings <b> and </b> (the HTML tags that mean "bold").

  • 3

    Use html() to wrap the return value of goodbye() in the strings <i> and </i> (the HTML tags that mean "italics").

  • 4

    Use html() to wrap hello_goodbye() in a DIV, which is done by adding the strings <div> and </div> tags around a string.