Problème python
Fermé
kuskus2022
-
14 mai 2022 à 10:34
yg_be Messages postés 23350 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 novembre 2024 - 14 mai 2022 à 11:26
yg_be Messages postés 23350 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 novembre 2024 - 14 mai 2022 à 11:26
1 réponse
yg_be
Messages postés
23350
Date d'inscription
lundi 9 juin 2008
Statut
Contributeur
Dernière intervention
26 novembre 2024
Ambassadeur
1 554
14 mai 2022 à 10:57
14 mai 2022 à 10:57
14 mai 2022 à 11:05
if __name__ == '__main__': # Do not modify or remove this line!
all_restaurants = get_michelin_restaurants()
# Your restaurant is in Bangkok and you want to know who else serves Innovative or Thai types of cuisine:
similar_restaurants = filter_list(all_restaurants, ['Innovative', 'Thai'], 2)
# Print the results:
print("Number of 2-star (or better) restaurants in Bangkok serving Innovative or Thai cuisine:", len(similar_restaurants), "(expected answer: 2)")
print("\nThis is the list produced by your code:")
print_restaurants(similar_restaurants, ['name', 'cuisine', 'stars'])
print("This is the expected list:")
print_restaurants([{'name':'Gaggan', 'cuisine': 'Innovative', 'stars': 2},
{'name':'Mezzaluna', 'cuisine': 'Innovative', 'stars': 2}], ['name', 'cuisine', 'stars'])
voila ce que j'ai dans ma console
TypeError Traceback (most recent call last)
<ipython-input-19-2ba75c5bbe7f> in <module>()
7
8 # Print the results:
----> 9 print("Number of 2-star (or better) restaurants in Bangkok serving Innovative or Thai cuisine:", len(similar_restaurants), "(expected answer: 2)")
10 print("\nThis is the list produced by your code:")
11 print_restaurants(similar_restaurants, ['name', 'cuisine', 'stars'])
TypeError: object of type 'NoneType' has no len()
si vous pouviez me donner une idée de par où commencer...
14 mai 2022 à 11:06
def print_restaurants(dict_list, key_list=None, max_items=-1):
# Prints a tabulated list of dictionaries line by line, each value on its own column.
# Argument key_list gives the keys for which the values shall be printed (default is all keys).
# Argument max_items is the maximal number of items to print (default is unlimited).
column_width = 20
# Stop here if the list is empty:
if len(dict_list) == 0:
return
# If no keys are specified, take those found in the first dictionary of the list:
if key_list is None:
key_list = dict_list[0].keys()
# Print a header line consisting of uppercase keys:
header = "\n"
for key in key_list:
header += str(key) + (' '*(column_width-len(str(key))))
print(header.upper())
# Print the values of the specified keys, nicely formatted by columns:
for dico in dict_list:
line = ''
for key in key_list:
value = str(dico[key])[:column_width-2]
line += value + (' '*(column_width-len(value)))
print(line)
max_items -= 1
if max_items == 0:
break
print()
14 mai 2022 à 11:24
14 mai 2022 à 11:26