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
Below is the pseudocode for this game:
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:
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:
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:
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:
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.