In this article, we are going to be using Python to create a simple text-based restaurant game. The game will present players with options for what they would like to order, and the program will take the user’s order and present them with a bill at the end.
Python Restaurant Game
This project is a great way to learn more about Python, as it is a relatively simple game to create, but has lots of potential for personalization, allowing you to make the game your own. It’s a great way to build both your creative thinking and problem-solving skills while making a functional computer program.
Who is this Project For?
This is a beginner-level project for those who are new to Python, or those who want a fun way to apply their coding skills. Before starting this project, you should have some experience formatting sentences, using if-statements, and taking user input. These are relatively simple concepts but will be used and built upon as you code this project.
What Will We Learn?
This program focuses on creating a functional restaurant game by using concepts such as if-statements, while loops, and taking user input to control the output of the game. These concepts set the foundation for many more complex Python concepts, so it is important to gain a strong understanding of them.
Features to Consider:
The user will be asked what they would like to order
Based on their selection, the user’s available budget will decrease
If the user does not select a valid option, they will be asked to try again
At the end of the game, players will be shown the bill for their order
Pseudo Code:
Below is the pseudocode for this game:
"""Create a money variable and assign it a valueCreate a list of drinks that customers can orderCreate a list of prices to be associated with all of the menu itemsAsk the user what they would like to drink, and reveal their optionsCreate a variable called validDrink and set it to falseWhile validDrink == false: If the user chooses one of the options, approve the purchase and subtract that value from their account Otherwise, ask the user to order againCreate a list of meals that customers can orderAsk the user what they would like to eat, and reveal their optionsCreate a variable called validMeal and set it to falseWhile validMeal == false: If the user chooses one of the options, approve the purchase and subtract that value from their account Otherwise, ask the user to order againCreate a list of desserts that customers can orderAsk the user what they would like to order for dessert, and reveal their optionsCreate a variable called validDesserts and set it to falseWhile validDesserts == false: If the user chooses one of the options, approve the purchase and subtract that value from their account Otherwise, ask the user to order againPrint the user’s bill, which includes the price for each choice they made along with the total for their order"""
Main Steps:
This project can be broken down into 3 main steps:
Ask the user to select a drink
Ask the user to select a meal
Ask the user to select a dessert
Display the bill
Step 1: Ask the User to Select a Drink
The first thing we’ll need to do when creating this game is to define a variable to store how much money the user has, create a list of drinks that the user can order from, and create a list to contain the prices of each menu item.
Hint: The pricing and list of foods could be done using a dictionary, but in order to keep this code very simple and perfect for first-time programmers, we have opted to use lists instead.
Then, we will ask the user what they would like to drink and show them their options. This will be done using formatting to make the options more convenient and clear to read. Finally, we will use a while loop to check if the user has made a valid choice. If so, the game will process, but if not, the user will be asked to enter a valid option. The code for this step can be seen below:
money =25.00
drinks = ["coffee", "Water", "coke", "iced tea"]
prices = [3.25, 1.00, 2.50, 3.00, 15.25, 18.75, 12.00, 10.00, 2.75, 5.00, 1.50]
drinkChoice =int(input("Hello and welcome to the Geekedu Cafe.
\nWhat would you like to drink/?" +"We have:\n[0] coffee..... 3.25\n[1] water..... 1.00\n[2] coke..... 2.50\n[3]
iced tea.....3.00\n"))
validDrink =Falsewhile validDrink ==False:
if drinkChoice==0:
money = money-(prices[0])
print("Okay, {} sounds perfect! You have ${} remaining.\n".format(drinks[0], money))
validDrink =Trueelif drinkChoice==1:
money = money-(prices[1])
print("Okay, {} sounds perfect! You have ${} remaining.\n".format(drinks[1], money))
validDrink =Trueelif drinkChoice==2:
money = money-(prices[2])
print("Okay, {} sounds perfect! You have ${} remaining.\n".format(drinks[2], money))
validDrink =Trueelif drinkChoice==3:
money = money-(prices[3])
print("Okay, {} sounds perfect! You have ${} remaining.\n".format(drinks[3], money))
validDrink =Trueelse:
drinkChoice =int(input("Sorry, that doesn't seem to be an option. Try again:\n"))
Step 2: Ask the User to Select a Meal
Next, we need to ask the user what they would like to order for the main course. This will be done in the same way as the drinks, except we will not be creating a list to store prices and will not be setting up the money variable. The code for this is as follows:
meals= ["spaghetti and meatballs", "steak and roasted vegetables", "burger and fries"
, "grilled cheese"]
mealChoice =int(input("Great! Now, what can I get you for the main course?\n
We have:\n[0] spaghetti and meatballs.....15.25\n[1] steak and roasted vegetables.....18.75\n[2] burger and fries.....12.00\n[3] grilled cheese.....10.00\n"))
validMeal =Falsewhile validMeal ==False:
if mealChoice ==0:
money = money-(prices[4])
print("Yum, {} sounds good! You have ${} remaining.\n".format(meals[0], money))
validMeal =Trueelif mealChoice ==1:
money = money-(prices[5])
print("Yum, {} sounds good! You have ${} remaining.\n".format(meals[1], money))
validMeal =Trueelif mealChoice ==2:
money = money-(prices[6])
print("Yum, {} sounds good! You have ${} remaining.\n".format(meals[2], money))
validMeal =Trueelif mealChoice ==3:
money = money-(prices[7])
print("Yum, {} sounds good! You have ${} remaining.\n".format(meals[3], money))
validMeal =Trueelse:
mealChoice =int(input("Sorry, that doesn't seem to be an option. Try again:\n"))
Note: You’re free to modify the meals, prices, and user’s money balance for your project. Feel free to make it your own by adding your favourite foods or customising the game however you wish!
Step 3: Ask the User to Select a Dessert
Now we need to do the same step again for the desserts. This will follow the same format as the code for the meal section, so once complete your code should look something like this:
desserts= ["chocolate chip cookie", "strawberry cheesecake", "icecream sundae"]
dessertsChoice =int(input("And finally, what would you like for dessert?\n
We have:\n[0] chocolate chip cookie.....2.75\n[1] strawberry cheesecake.....5.00
\n[2] icecream sundae.....1.50\n"))
validDesserts =Falsewhile validDesserts ==False:
if dessertsChoice ==0:
money = money-(prices[8])
print("One {} coming right up! You have ${} remaining.\n".format(desserts[0], money))
validDesserts =Trueelif dessertsChoice ==1:
money = money-(prices[9])
print("One {} coming right up! You have ${} remaining.\n".format(desserts[1], money))
validDesserts =Trueelif dessertsChoice ==2:
money = money-(prices[10])
print("One {} coming right up! You have ${} remaining.\n".format(desserts[2], money))
validDesserts =Trueelse:
dessertsChoice =int(input("Sorry, that doesn't seem to be an option. Try again:\n"))
Step 4: Display the Bill
Finally, we need to show the user the bill for their entire order. We will do this using a series of print statements. The first statement will thank the user for visiting and create a title for the bill. Then, we will print each of the user’s options and their associated prices. Finally, we will print the total amount that the user spends on their meal. This is the final section of code for this project, and once complete, it will look like this:
print("Thank you for visiting this restaurant!\n..... Your Bill .....")
print("{}.....${}\n{}.....${}\n{}.....${}\n".format(drinks[drinkChoice]
, prices[drinkChoice], meals[mealChoice], prices[mealChoice+4], desserts[dessertsChoice]
, prices[dessertsChoice+8]))
print("Total.....${}\n......................".format(25-money))
Project Complete!
Now the project is complete! We hope that you’ve enjoyed creating this simple text-based restaurant game. Feel free to test your code out and if you have any errors, make sure to check the code mentioned above as a reference.
Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.