Home > OS >  Restaurant Selector program
Restaurant Selector program

Time:12-11

I have a coding problem to solve. I did a half but I can't figure out rest.

Here is the problem:

You have a group of friends coming to visit for your high school reunions, and you want to take them out to eat at a local restaurant. You aren't sure if any of them have dietary restrictions, but your restaurant choices are as follows:

Joe's Gourmet Burgers - Vegetarian: No, Vegan: No, Gluten-Free: No
Main Street Pizza Company - Vegetarian: Yes, Vegan: No, Gluten-Free: Yes
Corner Cafe - Vegetarian: Yes, Vegan: Yes, Gluten-Free: Yes
Mama's Fine Italian - Vegetarian: Yes, Vegan: No, Gluten-Free: No
The Chef's Kitchen - Vegetarian: Yes, Vegan: Yes, Gluten-Free: Yes

Write a program that asks whether any numbers of your party are vegetarian, vegan, or gluten-free, to which then displays only the restaurants to which you may take the group.

Here is an example of the program's output:

Is anyone in your party a vegetarian ? yes
Is anyone in your party a vegan ? no
Is anyone in your party gluten-free ? yes
Here are your restaurant choices:
Main Street Pizza Company
Corner Cafe
The Chef's Kitchen

Here is another example of the program's output:

Is anyone in your party a vegetarian ? yes
Is anyone in your party a vegan ? yes
Is anyone in your party gluten-free ? yes
Here are your restaurants choices:
Corner Cafe
The Chef's Kitchen

Here is the code I wrote so far:

# Get the status of a party
vegetarian = input('Is anyone in your party a vegetarian(yes/no)? ')
vegan = input('Is anyone in your party a vegan(yes/no)? ')
gluten_free = input('Is anyone in your party gluten-free(yes/or)? ')

# Assign restaurants based on the status of a party
if vegetarian == 'yes' and vegan == 'yes' and gluten_free == 'yes':
    print("Here are your restaurant choices: \n"
          "Corner Cafe \n"
          "The Chef's Kitchen")
elif vegetarian == 'no' and vegan == 'no' and gluten_free == 'no':
    print("Here are your restaurant choices: \n"
          "Joe's Gourmet Burgers")

Here is the output:

Is anyone in your party a vegetarian(yes/no)? yes
Is anyone in your party a vegan(yes/no)? yes
Is anyone in your party gluten-free(yes/or)? yes
Here are your restaurant choices: 
Corner Cafe 
The Chef's Kitchen
Is anyone in your party a vegetarian(yes/no)? no
Is anyone in your party a vegan(yes/no)? no
Is anyone in your party gluten-free(yes/or)? no
Here are your restaurant choices: 
Joe's Gourmet Burgers

What I want is to write a statement that selects restaurants randomly based on the answers.
But I can figure out how.
Can someone tell me how to do it, please ??
I didn't google. I want to learn. Not to copy. Thank you in advance.

CodePudding user response:

preferences = []
d = {"yes": True, "no": False}

# fill up company preferences
preferences.append(d[input("Is anyone in your party a vegetarian(yes/no)? ")])
preferences.append(d[input("Is anyone in your party a vegan(yes/no)? ")])
preferences.append(d[input("Is anyone in your party gluten-free(yes/or)? ")])

restaurants = {  # initialize a dictionary of all restaurants
    "Joe's Gourmet Burgers": [False, False, False],
    "Main Street Pizza Company": [True, False, True],
    "Corner Cafe": [True, True, True],
    "Mama's Fine Italian": [True, False, False],
    "The Chef's Kitchen": [True, True, True]
}
print("Here are your restaurant choices:")
for restaurant, peculiarities in restaurants.items():  # iterate restaurants
    if all(map(lambda x: x[0] == x[1] or not x[1], zip(peculiarities, preferences))):  # if a restaurant is good for company print it
        print(restaurant)

CodePudding user response:

OOP: Model the restaurants as class instance with attributes. Provide a meaningful str().

Have your inputs as is, loop over list of Restaurant instances and put what is possible into a list.

Choose one randomly from list:

class Restaurant:
    # allow mostly named params, no positional ones
    def __init__(self, name, *, vegetarian=False, vegan=False, gluten_free=False):
        self.name = name
        self.vegetarian = vegetarian
        self.vegan = vegan
        self.gluten_free = gluten_free

    def __str__(self): 
        s = [ k[0] for k in zip( ["Vegetarian", "Vegan", "Gluten free"], 
            (self.vegetarian, self.vegan, self.gluten_free)) if k[1]]
        return f"{self.name} {'(' if s else ''}{', '.join(s)}{')' if s else ''}"  

Program using the class:

restaurants = [
    Restaurant("Joe's Gourmet Burgers"),
    Restaurant("Main Street Pizza Company", vegetarian=True),
    Restaurant("Corner Cafe", vegetarian=True, vegan=True, gluten_free=True),
    Restaurant("Mama's Fine Italian", vegetarian=True),
    Restaurant("The Chef's Kitchen",   vegetarian=True, vegan=True, gluten_free=True),
]

vegetarian = input('Is anyone in your party a vegetarian(y=yes)? ').strip().lower()[0] == "y"
vegan = input('Is anyone in your party a vegan(y=yes)? ').strip().lower()[0] == "y"
gluten_free = input('Is anyone in your party gluten-free(y=yes)? ').strip().lower()[0] == "y"
print()
all_r = []
for r in restaurants:
    # vegetarians can eat vegan, meat lovers can eat vegetarian or vegan
    if (not gluten_free or r.gluten_free) and (not vegan or r.vegan) and (not vegetarian or r.vegetarian or r.vegan):
        print(r)
        all_r.append(r) # add to list for random choice
    
import random
print("\nWe decided on: ", random.choice(all_r)) # choose one from list random

Output of 2 runs:

Is anyone in your party a vegetarian(y=yes)? n
Is anyone in your party a vegan(y=yes)? n
Is anyone in your party gluten-free(y=yes)? n

Joe's Gourmet Burgers
Main Street Pizza Company (Vegetarian)
Corner Cafe (Vegetarian, Vegan, Gluten free)
Mama's Fine Italian (Vegetarian)
The Chef's Kitchen (Vegetarian, Vegan, Gluten free)

We decided on:  Joe's Gourmet Burgers



Is anyone in your party a vegetarian(y=yes)? y
Is anyone in your party a vegan(y=yes)? n
Is anyone in your party gluten-free(y=yes)? y

Corner Cafe (Vegetarian, Vegan, Gluten free)
The Chef's Kitchen (Vegetarian, Vegan, Gluten free)

We decided on:  The Chef's Kitchen (Vegetarian, Vegan, Gluten free)
  • Related