I've been tasked with defining a function that iterates over a list that contains two dictionaries. It must iterate over the list and return a newly populated list that contains the name of the restaurants that are not closed. Im having trouble 1. accessing the information (I think) 2. understanding when I need to define variables due to the errors that I receive. FYI the code that this function is passed through is already defined for me.
My code:
In[1]: restaurants = [fork_fig, frontier_restaurant]
In [2]: def open_restaurants(restaurants):
for sublsit in restaurants:
op=[]
if sublist.get('is_closed')==False:
op.append(sublist.get('name'))
passed through their code:
In [1]: len(open_restaurants(restaurants))
Out[2]: NameErrorTraceback (most recent call last)
<ipython-input-14-e78afc732a29> in <module>
----> 1 len(open_restaurants(restaurants)) # 1
<ipython-input-13-56ad484d6dd9> in open_restaurants(restaurants)
2 for sublsit in restaurants:
3 op=[]
----> 4 if sublist.get('is_closed')==False:
5 op.append(sublist.get('name'))
6
NameError: name 'sublist' is not defined
and next: I think this is because the list i defined is not populating with the correct information.
In[3]: open_restaurants(restaurants)[0]['name'] # 'Fork & Fig'
Out[4]:TypeErrorTraceback (most recent call last)
<ipython-input-15-bf5607d088f4> in <module>
----> 1 open_restaurants(restaurants)[0]['name'] # 'Fork & Fig'
TypeError: 'NoneType' object is not subscriptable
Aucun commentaire:
Enregistrer un commentaire