Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Utilizing Control Flow Structures in Python

To be effective with Python, one needs to be comfortable using the control flow structures it provides, either to take actions or to perform the same action multiple times. In this hands-on lab, we'll be utilizing control flow structures, like `if` statements and loops, to fix some failing automated tests, and ensure that the application works correctly. By the time we're finished with this hands-on lab, we should be more comfortable using `if` statements and `for` loops to write more robust functions.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Apr 03, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Correct the `take_first` Function

    Currently, the take_first function will raise an error if it receives an empty list of todos, but we'd like to handle that differently. Our doctests show that we should instead be returning (None, todos) if todos is empty. There is more than one way that we could go about doing this:

    1. Use a conditional statement to see if todos is empty right away. If it is, then return (None, todos).
    2. Use a try statement, and if an error is raised return (None, todos).

    For simplicity's sake let's use a conditional, instead of error handling, as control flow:

    def take_first(todos):
        """
        take_first receives a list of todos and removes the first todo
        and returns that todo and the remaining todos in a tuple
    
        >>> todos = [{'name': 'Example 1', 'body': 'This is a test task', 'points': '3'},
        ... {'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'}]
        >>> todo, todos = take_first(todos)
        >>> todo
        {'name': 'Example 1', 'body': 'This is a test task', 'points': '3'}
        >>> todos
        [{'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'}]
        >>> todos = []
        >>> take_first(todos)
        (None, [])
        """
        if todos:
            todo = todos.pop(0)
            return (todo, todos)
        else:
            return (None, todos)
    
  2. Challenge

    Correct the `sum_points` Function

    The original version of sum_points takes two todo dictionaries and returns the sum of their point values. But that's not very useful. For us to make this work for us, we're going to modify the function so that it will take a list of todo dictionaries and sum all of the point values. We can do this easily, using a for loop and an accumulator:

    def sum_points(todos):
        """
        sum_points receives two todo dictionaries and returns the sum of their `point` values.
    
        >>> todos = [{'name': 'Example 1', 'body': 'This is a test task', 'points': '3'},
        ... {'name': 'Task 2', 'body': 'Yet another example task', 'points': '2'},
        ... {'name': 'Task 3', 'body': 'Third task', 'points': '5'}]
        >>> sum_points(todos)
        10
        """
        total = 0
        for todo in todos:
            total += int(todo['points'])
        return total
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans