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
Bonjour,

je dois créer une liste (a new list containing only restaurants matching all the given constraints) de restaurant a partir de ca :

{
'name': 'Garibaldi', # Name of the restaurant
'year': 2018, # Awarded year
'latitude': 1.296564, # Its location (North-South)
'longitude': 103.855, # Its location (East-West)
'city': 'Singapore', # City name
'region': 'Singapore', # State or country
'zipCode': 'n/a', # Postal code
'cuisine': 'Italian', # Style
'price': '$', # Price range (from $ to $$$$$)
'url': 'https://guide.michelin.com/sg/en/singapore-region/singapore/restaurant/garibaldi',
# URL of page in online Michelin guide
'stars': 1 # Stars awarded by Michelin (from 1 to 3)
}

et ca
if __name__ == '__main__': # Do not modify or remove this line!

all_restaurants = get_michelin_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'])

Je n'y arrive pas du tout, quelqu'un a une idée ?

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
0
Mon problème est que je ne comprends pas ce que c'est un len() ni comment défénir ma fonction ni pourquoi j'aurai une expected answer...

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...
0
kuskus2022 > kuskus2022
14 mai 2022 à 11:06
j'ai réussi cette partie deja

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()
0
yg_be Messages postés 23350 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 novembre 2024 1 554 > kuskus2022
14 mai 2022 à 11:24
as-tu lu les explications que je t'ai donné à propos de l'utilisation des balises de code?
0
yg_be Messages postés 23350 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 novembre 2024 1 554 > kuskus2022
14 mai 2022 à 11:26
0