Question: IN PYTHON: (g.1) Modify the program so that whenever a Restaurant is displayed, an additional line is included: Average price: $12.45. Average calories: 455.6 Follow
IN PYTHON:
(g.1) Modify the program so that whenever a Restaurant is displayed, an additional line is included:
Average price: $12.45. Average calories: 455.6
Follow the formatting shown above.
(g.2) Add a command to the main menu that allows the user to search for all the restaurants that serve a specified cuisine and display them along with the average price of (all the menus of the restaurants that serve) that cuisine.
(g.3) Add a command to the main menu that allows the user to search for (and display) all the restaurants that serve a dish whose name contains a given word or phrase. (This is more realistic than forcing the user to type the exact name of the dish; here, at least, the user can just type "fava beans" and match all the dishes that include that phrase.)
# RESTAURANT COLLECTION PROGRAM
# Implement Restaurant as a namedtuple, collection as a list
##### MAIN PROGRAM (CONTROLLER)
def restaurants(): # nothing -> interaction
""" Main program
"""
print("Welcome to the restaurants program!")
our_rests = collection_new()
our_rests = handle_commands(our_rests)
print(" Thank you. Good-bye.")
MENU = """
Restaurant Collection Program --- Choose one
n: Add a new restaurant to the collection
r: Remove a restaurant from the collection
s: Search the collection for selected restaurants
p: Print all the restaurants
q: Quit
e: Remove (erase) all the restaurants from the collection
c: Change prices for the dishes served
"""
def handle_commands(diners: list) -> list:
""" Display menu, accept and process commands.
"""
done = False
while not done:
response = input(MENU)
if response=="q":
done = True
return diners
elif response=='n':
r = restaurant_get_info()
diners = collection_add(diners, r)
elif response=='r':
n = input("Please enter the name of the restaurant to remove: ")
diners = collection_remove_by_name(diners, n)
elif response=='p':
print(collection_str(diners))
elif response=='s':
n = input("Please enter the name of the restaurant to search for: ")
for r in collection_search_by_name(diners, n):
print(restaurant_str(r))
elif response =='e':
print(delete_everything(diners))
elif response =='c':
change_of_price= input("number to change dish prices: ")
print(Restaurant_change_price(diners,float(change_of_price)))
else:
invalid_command(response)
def invalid_command(response): # string -> interaction
""" Print message for invalid menu command.
"""
print("Sorry; '" + response + "' isn't a valid command. Please try again.")
##### Restaurant
from collections import namedtuple
Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
# Constructor: r1 = Restaurant('Taillevent', 'French', '01-11-22-33-44', 'Escargots', 23.50)
def restaurant_str(self: Restaurant) -> str:
return (
"Name: " + self.name + " " +
"Cuisine: " + self.cuisine + " " +
"Phone: " + self.phone + " " +
"Dish: " + self.dish + " " +
"Price: ${:2.2f}".format(self.price) + " ")
def restaurant_get_info() -> Restaurant:
""" Prompt user for fields of Restaurant; creat and return.
"""
return Restaurant(
input("Please enter the restaurant's name: "),
input("Please enter the kind of food served: "),
input("Please enter the phone number: "),
input("Please enter the name of the best dish: "),
float(input("Please enter the price of that dish: ")))
#### COLLECTION
# A collection is a list of restaurants
def collection_new() -> list:
''' Return a new, empty collection
'''
return [ ]
def collection_str(diner_collection: list) -> str:
''' Return a string representing the collection
'''
s = ""
for r in diner_collection:
s = s + restaurant_str(r)
return s
def collection_search_by_name(diner_list: list, diner_name: str) -> list:
""" Return list of Restaurants in input list whose name matches input string.
"""
result = [ ]
for r in diner_list:
if r.name == diner_name:
result.append(r)
return result
def collection_add(diner_list: list, diner: Restaurant) -> list:
""" Return list of Restaurants with input Restaurant added at end.
"""
diner_list.append(diner)
return diner_list
def collection_remove_by_name(diners: list, diner_name: str) -> list:
""" Given name, remove all Restaurants with that name from collection.
"""
result = [ ]
for r in diners:
if r.name != diner_name:
result.append(r)
return result
def delete_everything(dinners:list)-> list:
return dinners.clear()
def Restaurant_change_price(x:list,y:float)->list:
new=[]
for a in x:
new.append(a.price*(y/100)+(a.price))
return new
restaurants()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
