Function Arguments Lab


In this lesson, we have decided to visit another of our travel destinations! This time we have chosen to visit Albuquerque, but we aren't very familiar with this city and are quite hungry after our long flight. We will be working with information we pulled from the Yelp database to help us find a restaurant where we can satisfy our hunger. While Yelp is great for learning about what to do in Albuquerque, it gives us back a lot of information. We'll use what we know about functions and dictionaries to format and read our data more easily.

Exploring Two Restaurants in Albuquerque

Let's take a quick look at the information Yelp provides for a single restaurant:

fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},
  {'alias': 'sandwiches', 'title': 'Sandwiches'},
  {'alias': 'salad', 'title': 'Salad'}],
 'coordinates': {'latitude': 35.10871, 'longitude': -106.56739},
 'display_phone': '(505) 881-5293',
 'distance': 3571.724649307866,
 'id': 'fork-and-fig-albuquerque',
 'image_url': '',
 'is_closed': False,
 'location': {'address1': '6904 Menaul Blvd NE',
  'address2': 'Ste C',
  'address3': '',
  'city': 'Albuquerque',
  'country': 'US',
  'display_address': ['6904 Menaul Blvd NE', 'Ste C', 'Albuquerque, NM 87110'],
  'state': 'NM',
  'zip_code': '87110'},
 'name': 'Fork & Fig',
 'phone': '+15058815293',
 'price': '$$',
 'rating': 4.5,
 'review_count': 604,
 'transactions': [],
 'url': ''}

Above is the information provided about Fork & Fig, but all restaurants are provided with this information. For example, here is the information provided by Yelp for another restaurant, Frontier Restaurant.

frontier_restaurant = {'categories': [{'alias': 'mexican', 'title': 'Mexican'},
  {'alias': 'diners', 'title': 'Diners'},
  {'alias': 'tradamerican', 'title': 'American (Traditional)'}],
 'coordinates': {'latitude': 35.0808088832532, 'longitude': -106.619402244687},
 'display_phone': '(505) 266-0550',
 'distance': 4033.6583235266075,
 'id': 'frontier-restaurant-albuquerque-2',
 'image_url': '',
 'is_closed': False,
 'location': {'address1': '2400 Central Ave SE',
  'address2': '',
  'address3': '',
  'city': 'Albuquerque',
  'country': 'US',
  'display_address': ['2400 Central Ave SE', 'Albuquerque, NM 87106'],
  'state': 'NM',
  'zip_code': '87106'},
 'name': 'Frontier Restaurant',
 'phone': '+15052660550',
 'price': '$',
 'rating': 4.0,
 'review_count': 1369,
 'transactions': [],
 'url': ''}

As we already know, one way to quickly view the attributes of a dictionary is to look at the keys of the dictionary.

fork_fig.keys() == frontier_restaurant.keys()

As we can see from our above comparison, Yelp provides us with the same information for both restaurants.

Writing our functions

Ok, now let's write our functions. Write a function called restaurant_name that, provided a dictionary representing a restaurant like you saw above, returns that restaurant's name.

def restaurant_name(restaurant):
restaurant_name(frontier_restaurant) # 'Frontier Restaurant'
restaurant_name(fork_fig) # 'Fork & Fig'

Now write a function called restaurant_rating that returns the rating of the provided restaurant.

def restaurant_rating(restaurant):
restaurant_rating(frontier_restaurant) # 4.0
restaurant_rating(fork_fig) # 4.5

Comparing restaurants

Now let's write a function called is_better that returns True if a restaurant has a higher rating than an alternative restaurant. The first argument should be called restaurant and the second argument should be called alternative. The function returns False if the two ratings are equal.

def is_better(restaurant, alternative):
is_better(frontier_restaurant, fork_fig) # False
is_better(fork_fig, frontier_restaurant) # True
is_better(fork_fig, fork_fig) # False

Now let's write a function called is_cheaper that returns True if a restaurant has a lower price, that is the restaurant has fewer '$' signs, than an alternative restaurant. The first argument should be called restaurant and the second argument should be called alternative. The function returns False if the two prices are equal.

Hint: Strings in Python respond to then len function.

def is_cheaper(restaurant, alternative):
is_cheaper(fork_fig, frontier_restaurant) # False
is_cheaper(frontier_restaurant, fork_fig) # True
is_cheaper(fork_fig, fork_fig) # False

Now write a function called high_rating that takes a restaurant as a first argument and a rating (in the form of a number) as the second argument and returns True if the given restaurant's rating is greater than or equal to the provided rating and returns False otherwise.

def high_rating(restaurant, rating):
high_rating(fork_fig, 4) # True
high_rating(fork_fig, 5) # False
high_rating(frontier_restaurant, 4) # True


Great! In this lab we saw how to pass both single and multiple arguments to functions.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us