Database Challenges
After completing the code-along, attempt the challenges below. These exercises will reinforce the database skills from the code-along.
After working on the challenges for a while, feel free to start working on the final project.
Challenge 1: Adding a 'Date of Visit' Property to Review Objects
In this challenge, add a new piece of data to store with each Restaurant Review object: the date that the reviewer visited the restaurant. For the purposes of this exercise, the date will be stored as a simple string.
Part A: Updating the add_review
Function
First, update the add_review
function so that it can handle the new "date of visit" value.
- Open the reviews.py file for editing
- Find the
add_review
function - Add a parameter to the function:
date_of_visit
- Find the new dictionary that is added to the database with
db[new_db_key]
- Add a new key to the dictionary:
'date_of_visit'
- Set the value for the
'date_of_visit'
key to be thedate_of_visit
parameter
Run the program, and try adding a new review. At this point, it should fail - the call to the add_review
function does not have the proper arguments!
Part B: Updating the prompt_for_add_review
Function
Now that the add_review
function can handle the new property, make sure the user is asked for it as well.
- Open the main.py file for editing
- Find the
prompt_for_add_review
function - Under the
text
variable, create a new variable nameddate_of_visit
- Set the
date_of_visit
variable to aninput
asking what date the user visited the restaurant - Pass the
date_of_visit
variable into theadd_review
function call
Run the program again, and now try adding a new review. It should work! The date_of_visit
value should be stored in the database. But it is currently not possible to see the information anywhere.
Part C: Updating the print_reviews
Function
Next, make sure the user can actually see each review date when they view all reviews.
- Open the main.py file for editing
- Find the
print_reviews
function - Find the
for
loop that loops throughreview_list
- Make a new line under the line that prints out the score for the
review
- On that line, print out the date of visit for the review, e.g.:
Date of Visit: Oct 10, 2010
Run the program once more, print all reviews, and verify that it is possible to see the date of visit for each new review!
Challenge 2: Adding a Command to Print Certain Restaurant Reviews
In this challenge, add a command to the application that allows the user to select a certain restaurant, and see reviews only for that restaurant. Note that there are several different ways to accomplish this, so feel free to do whatever works.
Part A: Defining the Function
First, define a new function that will execute the command.
- Open the main.py file for editing
- Under the
print_reviews
function definition, define a new function namedprint_reviews_for_restaurant
- In the body of the
print_reviews_for_restaurant
function, addos.system('clear')
to clear the console - Next, create a variable named
restaurant_filter
- Set
restaurant_filter
to aninput
asking which restaurant to find reviews for - Under that, get the reviews by using
reviews.get_reviews()
- Store the result in a variable named
reviews
- Store the result in a variable named
- Under that, create a
for
loop that loops through eachreview
inreviews
- In the body of the
for
loop, check if the'restaurant'
for thereview
matchesrestaurant_filter
- If it does, print out the review
- If it does not, do nothing
Now the function is defined, but it will not do anything until it is called.
Part B: Adding the Command
Next, add a command in the while True
loop to handle printing reviews for a certain restaurant.
- At the bottom of the menu, print out
[1] View Reviews for Restaurant
- Create an
elif
that checks if the user entered1
- If they did, call the
print_reviews_for_restaurant
function
Run the program, and verify that it is possible to see all reviews for only one restaurant!
(BONUS) Part C: Refactoring
Try to figure out a different way to create this functionality. Currently, some code is being repeated that prints out each restaurant. See if it is possible to make this work better.
Challenge 3: Handling Improper Scores
Currently, it is possible to enter any score for a review. It should only be possible to enter a number between 0
and 10
. Fix the code to prevent the user from entering a score outside of those boundaries.
- Open the main.py file for editing
- Find the
prompt_for_add_review
function - Find where the
score
variable is set in the function - Under that, create an
if
statement checking ifscore
is outside the range of0
and10
- If it is, print a message saying "ERROR" and
return
- Make a similar change to the
prompt_for_update_review
function
Run the program, and verify that it is impossible to enter a score that is greater than 10
, or less than 0
.
(BONUS) No Non-numeric Input
Currently, if the user enters something other than a number for the score, the program crashes. Fix it so that the error is handled more gracefully. This may require some research.
(BONUS) Challenge 4: Sorting the Reviews
In the current version of the app, the reviews are not printed in any particular order. Figure out how to sort them in a couple different ways:
- Alphabetically by restaurant name
- Chronologically by date of visit
- Ranked by score
The program should ask the user how to sort the reviews before printing them.
(BONUS) Challenge 5: Adding Authentication
At this point, any person who enters the application will be able to add a review. Fix it so that a user must be logged in to add reviews. There are several ways to do this, but here is some guidance:
- Add a
user
property to each object in the database - Store a list of users in the database
- Allow users to create an account
- Only allow users to edit their own reviews
- Create a log-in mechanism where users enter their name and a password
- Allow a user to see their own reviews
Feel free to learn more about authentication before diving into this challenge.
Beyond
There are infinite possibilities with the Replit DB. Try to build a completely new application, possibly a Flask web app, a Pygame game, or a Discord bot, that uses a database. This can also lead directly into the final project.