6.100L | Fall 2022 | Undergraduate

Introduction to CS and Programming using Python

Lecture 13: Exceptions, Assertions

Topics: Exceptions, assertions

Lecture Notes

Lecture 13: Exceptions, Assertions

Lecture 13 Code

Readings

Ch 9

Finger Exercise Lecture 13

Implement the function that meets the specifications below:

def sum_str_lengths(L):
    """
    L is a non-empty list containing either: 
    * string elements or 
    * a non-empty sublist of string elements
    Returns the sum of the length of all strings in L and 
    lengths of strings in the sublists of L. If L contains an 
    element that is not a string or a list, or L's sublists 
    contain an element that is not a string, raise a ValueError.
    """
    # Your code here  

# Examples:
print(sum_str_lengths(["abcd", ["e", "fg"]]))  # prints 7
print(sum_str_lengths([12, ["e", "fg"]]))      # raises ValueError
print(sum_str_lengths(["abcd", [3, "fg"]]))    # raises ValueError

6.100L Finger Exercises Lecture 13 Solutions

Course Info

Instructor
As Taught In
Fall 2022
Learning Resource Types
Lecture Videos
Lecture Notes
Problem Sets
Programming Assignments with Examples
Readings
Recitation Notes