Interactive Research Study on Python Objects

You will be asked to construct a Python function by selecting and placing scrambled lines of code so that the function works correctly.

Before we present the problem, click on the button below to run a practice problem. This problem will give you an opportunity to learn how to solve a problem by placing lines of Python code in the right order and the right level of indentation.

Practice Code Scramble Exercise

Arrange the code in the right hand box so that it defines a function called print_long_words. This function should have one parameter that is a list of strings. When called, the function should print all of the strings that are longer than 6 characters. After printing the long strings, it should print the message "Those are the long words!".

Below are example runs:

        >>> print_long_words(['cat', 'aardvark', 'leopard', 'horse'])
        aardvark
        leopard
        Those are the long words!
      
        >>> print_long_words(['emu', 'albatross'])
        albatross
        Those are the long words!
        >>> 
      

There is one line of code that is not used in the solution.

Remember to pay attention to indentation!

Trash

Solution

Once you are done practicing, click on the button below to learn about the problem for this study.

Problem Overview

The problem involves search through a list of objects that represent rooms at an inn. Below is a table presenting example descriptions:

Name Label Occupancy Size View
Stockton A3 4 medium river
Bassword B2 2 small river
Manitou C1 4 large village
Eagle A4 5 large mountain

A class called Room has already been defined with the properties listed in the table. The properties can be accessed using dot-notation with the name of the property in lowercase. For example, if new_room is a Room object, its size is accessed with the expression new_room.size.

Below is an example function that takes a list of room objects and prints all of their view properties:

    def display_room_views(rlist):
       'displays all of the views for the rooms in rlist'
       for room in rlist:
          print(room.view)
    

Here is a function that takes a list of room objects and returns a new list of room objects that have an occupancy of greater than 4:

    def get_high_occupancy_rooms(rlist):
       'return a list of rooms from rlist with occupancy more the 4'
       new_list = []
       for r in rlist:
          if r.occupancy > 4:
             new_list.append(r)
       return new_list
    

Before you work on the problem, you are encouraged to study the above examples now since you will not be able to view them after starting the problem. When you are ready to work on the problem, click on the button below.

Problem: construct a new function

Select and arrange the scrambled lines of code so that it defines a new function called lookup. This function takes two parameters: a list of room objects and a string that indicates the view property. This function should return the name of the first room whose view matches the given property. For example:

            lookup(room_list, "mountain")
        
might return the string "Eagle".

There are two lines of code that are not used in the solution.

Remember to pay attention to indentation!

Trash

Solution