I recently finished the Udemy Course, 2022 Complete Python Bootcamp from Zero to Hero from Jose Portilla. This is my capstone project for the course. This project was to not only demonstrate everything I learned in Python which includes: python object and data structure basics, object oriented programming, and errors and exception handling. Just to name a few. In this article I will discuss my struggles with the project and explain in detail how it works. This isn't an explanation or introduction to Python but I do plan on publishing an article on that in the future.
Background
Why create a program that takes more time than it would to actually choose a place to eat? Besides the obvious of wanting to practice my Python skills. It's because my friends and I are notoriously indecisive. We are terrible at making decisions. Especially when it comes to choosing a place to eat. One of my friends had this method for picking a place to eat which was a basis for this idea. It goes like this:
Person A: Says three categories (cuisine) of food like Asian, American and Italian
Person B: Chooses the category they want: American.
Person B: Then gives three places to eat within that category: In-N-Out, Cane's or McDonald's.
Person A: Picks one of the three choices.
But this takes time and requires brain power for each person. Instead I wanted to create a Python program based off of this.
This program is dead simple. There is no option for the program to ask the user for their own restaurant choice (at least right now though this maybe something I do in the future). Why? Because I made this program for me. To solve a problem I have. I included restaurants I frequent along with what I consider "cheap" or "expensive". I also wanted to complete a project that was challenging yet doable with my current skill level. I may expand this program in the future but this is the first edition.
There is no option to replay the picker. Because that's the whole dilemma. Once it picks. It's done. That's it. Live with the decision.
How it Works
The user will be able to choose two things:
How much you want to spend on a scale of 1-3: 1 = cheap, 2 = not bad, or 3 = expensive (this is all relevant to me and my own budget);
The type of cuisine (i.e. American, Mexican, Japanese, etc.) you want based on the list that's available. These cuisines are listed for the user to see.
Then the program takes these parameters and randomly picks a restaurant.
Steps
I broke my game up into simple steps and had a jupyter notebook that separated these steps. I also tested my code in this notebook. If you're interested to see this notebook feel free to email me at: kelly@kellyjadams.com.
Import and Global Variables
Class Definition
Create Objects for Each Restaurant
Create a List of all the Restaurants
Defined my Functions
sepending_input()
cuisine_input()
decisions()
Program Logic
Import and Global Variables
Import
I first imported the random module which I used to later choose the restaurant randomly.
import random
Global Variables
Then I declared global variables to store cuisine type, expense, and price. The cuisine and expense variables are tuples. And the price variable is a dictionary.
cuisine = ('American', 'Asian', 'Mexican')
expense = ('Cheap', 'Not Bad', 'Expensive')
price = {'Cheap':1 , 'Not Bad': 2, 'Expensive':3 }
Class Definition
Next I created a restaurant class where each restaurant object has a cuisine type, expense (how expensive it is), the actual price based on a 1-3 scale, and its name.
I used the __str__ method to return the name represented as a string. The __repr__ returns the string representation of an object and was mainly used when I wanted to print out a list of the objects.
class restaurant:
def __init__(self, cuisine, expense, name):
self.cuisine = cuisine
self.expense = expense
self.price = price[expense]
self.name = name
# The string representation
def __str__(self):
return self.name
# Returns the string representation of an ojbect
def __repr__(self):
return str(self)
Created Objects for Each Restaurant
I created objects for each restaurant. Each object uses the restaurant class and contains the cuisine type, expense, and name (in string form). All of these restaurants are ones I frequent. Also, the definition for expense (whether or not I think something is cheap) is personal based on my own values and financial situation.
I made sure to list several different types of restaurants for each cuisine type (so the program has a few choices to pick from randomly). I only had three cuisine types for simplicity but I can expand and add other types of cuisine in the future. This part of the program is scalable.
# American
inNOut = restaurant(cuisine ='American', expense = 'Cheap', name='In N Out')
kfc = restaurant(cuisine ='American', expense='Cheap', name='KFC')
canes = restaurant(cuisine ='American', expense='Cheap', name='Raising Canes')
chilis = restaurant(cuisine ='American', expense='Not Bad', name="Chili's")
blackbear = restaurant(cuisine ='American', expense='Not Bad', name="Black Bear Dinner")
twoChicks = restaurant(cuisine ='American', expense='Not Bad', name="Two Chicks")
bjsBrew = restaurant(cuisine ='American', expense='Expensive', name="BJ's Brewhouse")
wildRiver = restaurant(cuisine ='American', expense='Expensive', name="Wild River Grille")
# Asian
panda = restaurant(cuisine ='Asian', expense='Cheap', name='Panda Express')
pokeking = restaurant(cuisine ='Asian', expense='Cheap', name='Poke King')
pho777 = restaurant(cuisine ='Asian', expense='Cheap', name='Pho 777')
goldenFlower = restaurant(cuisine ='Asian', expense='Not Bad', name='Golden Flower')
ramen4real = restaurant(cuisine ='Asian', expense='Not Bad', name='Ramen4Real')
redBloom = restaurant(cuisine ='Asian', expense='Not Bad', name='Red Bloom')
ijji2 = restaurant(cuisine ='Asian', expense='Expensive', name='Ijji 2')
ijji4 = restaurant(cuisine ='Asian', expense='Expensive', name='Ijji 4')
# Mexican
tacoBell = restaurant(cuisine ='Mexican', expense='Cheap', name = 'Taco Bell')
losTresToritos = restaurant(cuisine ='Mexican', expense='Cheap', name = 'Los Tres Toritos')
chipotle = restaurant(cuisine ='Mexican', expense='Cheap', name='Chipotle')
miguels = restaurant(cuisine ='Mexican', expense='Not Bad', name="Miguel's")
tacosElRey = restaurant(cuisine ='Mexican', expense='Not Bad', name="Tacos El Rey")
Create a List of all the Restaurants
Then I created a list which holds all of these restaurant objects. If you print out the list it will print out the names (string) of each of the objects (which is using the __repr__ function in the Restaurant class definition). I did this for readability.
all_restaurants = [
inNOut,
kfc,
canes,
chillis,
blackbear,
twoChicks,
bjsBrew,
wildRiver,
panda,
pokeking,
pho777,
goldenFlower,
ramen4real,
redBloom,
ijji2,
ijji4,
tacoBell,
losTresToritos,
chipotle,
miguels,
tacosElRey
]
Defined my Functions
spending_input()
Ask the user for their input on how much they want to spend. I used the try/except method because I was asking the user for an integer value. I also wanted to practice the try/except method. If they input the correct value (1-3) then it will return the string version of that value. If they input a number outside of that it will return "I'm sorry please pick a number 1-3". Or if the user generates a value error then it will say something similar.
def spending_input():
while True:
try:
money = int(input('How much do you want to spend? On a scale from 1-3. With 1 being cheap, 2 being not so bad, and 3 being expensive. '))
if money == 1:
return 'Cheap'
elif money == 2:
return 'Not Bad'
elif money == 3:
return 'Expensive'
else:
return "I'm sorry please pick a number 1-3. "
except ValueError:
print ("Please input a number 1-3.")
cuisine_input()
Ask the user for the type of cuisine they want. First a created a variable and set it to a string (the contents of the string doesn't matter). I also printed the types of cuisines they have to choose from with the list of cuisines (global variable).
Next there is a while loop. Which continually asks the user to choose the cuisine they want. If the choice/input is not in that cuisine list then it keeps asking. If they do choose a cuisine in that cuisine list then it returns the cuisine.
def cuisine_input():
choice = 'wrong'
print("\nThese are the types of cuisines you have to choose from: " + str(cuisine))
while choice not in cuisine:
choice = input("Please choose the cuisine you want. ")
if choice not in cuisine:
print("Sorry that cuisine is not listed. Please choose another one. ")
else:
return choice
decision(money, food)
Using what the user has inputted expense = money and cuisine = food to create a list restaurants that satisfy these conditions . I created an empty list in the function called a. Then it goes through one-by-one (iterates) each restaurant in the all_restaurants list . If a restaurant in the list has the same expense and cuisine type as the user specified then it will add (append) it to the a list. Once it goes through the entire restaurant list it will return that list of restaurants that satisfy the conditions given by the user (cuisine and expense).
def decision(money, food):
a = []
# choose correct list
for x in all_restaurants:
if x.cuisine == food and x.expense == money:
a.append(x)
return a
Program Logic
Finally it's onto the actual logic of the program using everything I wrote about. First I have a simple print statement with "Welcome to Restaurant Picker". Then I have a function called restaurant_picker(). I call the spending_input() function which asks the user for how much they want to spend. I set that value it returns to a variable called cash. Next I call the function cuisine_input(), which asks the user how much they want to spend. I set that value it returns to a variable called restaurant.
Then I create a list of choices using the decision() function based on the user input of cash and cuisine type. I set the list it returns to a variable called final_choice. Then using the random module I choose a random restaurant from the final_choice list. That random restaurant is set to a variable called final_decision.
Finally I print out the final_decision.
print('Welcome to Restaurant Picker!\n')
def restaurant_picker():
# Ask for how much the user wants to spend
cash = spending_input()
# Ask for what type of cuisine the user wants
restaurant = cuisine_input()
#Create a list of choices based on the user input of cash and cuisine type
final_choice = decision(cash, restaurant)
# Choose randomly from the list above
final_decision = random.choice(final_choice)
# Print the final choice
print("\nThe restaurant for you is: " + str(final_decision))
restaurant_picker()
Final Code
The final code (which you can also view on Github here) is:
import random
# GLOBAL VARIABLES LIST
cuisine = ('American', 'Asian', 'Mexican')
expense = ('Cheap', 'Not Bad', 'Expensive')
price = {'Cheap':1 , 'Not Bad': 2, 'Expensive':3 }
# CLASS DEFINITION
class restaurant:
def __init__(self, cuisine, expense, name):
self.cuisine = cuisine
self.expense = expense
self.price = price[expense]
self.name = name
# The string representation
def __str__(self):
return self.name
# Returns the string representation of an ojbect
def __repr__(self):
return str(self)
# LIST OF RESTAURANTS (Seperated by Type)
# American
inNOut = restaurant(cuisine ='American', expense = 'Cheap', name='In N Out')
kfc = restaurant(cuisine ='American', expense='Cheap', name='KFC')
canes = restaurant(cuisine ='American', expense='Cheap', name='Raising Canes')
chilis = restaurant(cuisine ='American', expense='Not Bad', name="Chili's")
blackbear = restaurant(cuisine ='American', expense='Not Bad', name="Black Bear Dinner")
twoChicks = restaurant(cuisine ='American', expense='Not Bad', name="Two Chicks")
bjsBrew = restaurant(cuisine ='American', expense='Expensive', name="BJ's Brewhouse")
wildRiver = restaurant(cuisine ='American', expense='Expensive', name="Wild River Grille")
# Asian
panda = restaurant(cuisine ='Asian', expense='Cheap', name='Panda Express')
pokeking = restaurant(cuisine ='Asian', expense='Cheap', name='Poke King')
pho777 = restaurant(cuisine ='Asian', expense='Cheap', name='Pho 777')
goldenFlower = restaurant(cuisine ='Asian', expense='Not Bad', name='Golden Flower')
ramen4real = restaurant(cuisine ='Asian', expense='Not Bad', name='Ramen4Real')
redBloom = restaurant(cuisine ='Asian', expense='Not Bad', name='Red Bloom')
ijji2 = restaurant(cuisine ='Asian', expense='Expensive', name='Ijji 2')
ijji4 = restaurant(cuisine ='Asian', expense='Expensive', name='Ijji 4')
# Mexican
tacoBell = restaurant(cuisine ='Mexican', expense='Cheap', name = 'Taco Bell')
losTresToritos = restaurant(cuisine ='Mexican', expense='Cheap', name = 'Los Tres Toritos')
chipotle = restaurant(cuisine ='Mexican', expense='Cheap', name='Chipotle')
miguels = restaurant(cuisine ='Mexican', expense='Not Bad', name="Miguel's")
tacosElRey = restaurant(cuisine ='Mexican', expense='Not Bad', name="Tacos El Rey")
# CREATE A LIST OF ALL OF THE RESTAURANTS
all_restaurants = [
inNOut,
kfc,
canes,
chillis,
blackbear,
twoChicks,
bjsBrew,
wildRiver,
panda,
pokeking,
pho777,
goldenFlower,
ramen4real,
redBloom,
ijji2,
ijji4,
tacoBell,
losTresToritos,
chipotle,
miguels,
tacosElRey
]
# FUNCTIONS
# How much you want to spend
def spending_input():
while True:
try:
money = int(input('How much do you want to spend? On a scale from 1-3. With 1 being cheap, 2 being not so bad, and 3 being expensive. '))
if money == 1:
return 'Cheap'
elif money == 2:
return 'Not Bad'
elif money == 3:
return 'Expensive'
else:
return "I'm sorry please pick a number 1-3. "
except ValueError:
print ("Please input a number 1-3.")
# What type of cuisine you want
def cuisine_input():
choice = 'wrong'
print("\nThese are the types of cuisines you have to choose from: " + str(cuisine))
while choice not in cuisine:
choice = input("Please choose the cuisine you want. ")
if choice not in cuisine:
print("Sorry that cuisine is not listed. Please choose another one. ")
else:
return choice
# List possible choices based on user input
def decision(money, food):
a = []
# choose correct list
for x in all_restaurants:
if x.cuisine == food and x.expense == money:
a.append(x)
return a
# PROGRAM LOGIC
print('Welcome to Restaurant Picker!\n')
def restaurant_picker():
# Ask for how much the user wants to spend
cash = spending_input()
# Ask for what type of cuisine the user wants
restaurant = cuisine_input()
#Create a list of choices based on the user input of cash and cuisine type
final_choice = decision(cash, restaurant)
# Choose randomly from the list above
final_decision = random.choice(final_choice)
# Print the final choice
print("\nThe restaurant for you is: " + str(final_decision))
restaurant_picker()
Conclusion
This was a fun project to make. I wanted to make sure the project was challenging but not impossible for my skill level. Hence why I didn't add things like: webscrapping data or asking the user for their restaurant choice. These may be features in the future but not right now. The next time my friends ask where we want to go to eat I will use this program.