Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_restaurants(file):
- """ (file) -> (dict, dict, dict)
- Return a tuple of three dictionaries based on the information in the file:
- Georgie Porgie
- 87%
- $$$
- Canadian, Pub Food
- Queen St. Cafe
- 82%
- $
- Malaysian, Thai
- Dumplings R Us
- 71%
- $
- Chinese
- Mexican Grill
- 85%
- $$
- Mexican
- Deep Fried Everything
- 52%
- $
- Pub Food
- >>>read_restaurants('/Users/samantha/Desktop/Restaurant_List.txt')
- - a dict of {restaurant name: rating%}
- {'Georgie Porgie':87}
- - a dict of {price: list of restaurant names}
- {'$$$':[Georgie Porgie]}
- - a dict of {cusine: list of restaurant names}
- {'Canadian':'Georgie Porgie', 'Pub Food':'Georgie Porgie'}
- """
- #open file for reading only
- open_file = open(file, 'r')
- # lists to be completed by function
- name_to_rating = {}
- price_to_names = {'$': [], '$$': [], '$$$': [], '$$$$': []}
- cuisine_to_names = {}
- # read first line
- line = open_file.readline()
- #keep in while loop until the end of the file
- while line != '':
- #name of resturant variable
- name = line.strip('\n') #remove /n
- #Percent people like the resturant variable
- percent = open_file.readline().strip('%\n')
- #Add to dict. the name_to_rating key being name and the value being percent
- name_to_rating[name] = percent
- #How much it costs
- dollar = open_file.readline().strip('%\n')
- #Add value name to corresponding $ already present in dict.
- price_to_names[dollar].append(name)
- #kind_of_food the restaurant offers variable
- kind_of_food = open_file.readline().strip('%\n')
- # If statements are result of the resturant serving more than one type of food
- #If the resturant serves more than one type of food:
- if ',' in kind_of_food:
- food_types = kind_of_food.strip().split(', ')
- food_types.insert(1,name)
- food_types.insert(3,name)
- cuisine_to_names[food_types[0]] = list(food_types[1:2])
- cuisine_to_names[food_types[2]] = list(food_types[3:len(food_types)])
- #If the type of food served by this resturant is already present in dict.
- elif kind_of_food in cuisine_to_names:
- cuisine_to_names[kind_of_food].append(name)
- #If the resturant only offers one type of food and it is not already present in
- #the dict.
- else:
- cuisine_to_names[kind_of_food] = [name]
- empty_line = open_file.readline()
- line = open_file.readline()
- print(name_to_rating)
- print(price_to_names)
- print(cuisine_to_names)
- open_file.close()
Add Comment
Please, Sign In to add comment