20.5 Shelve Practice


Shelve Practice 1

Use the attached database. It should be called mydatabase.db . Using it, first get all the keys and put them into a list. For help on this, see the hint below. Next, sort the list. Finally, print the corresponding values. To do that, do print((shelfname)[key]) where (shelfname) is the name of your shelf and key is the key.
Hint: to get a dictionary or shelf’s keys, all you have to do is the following:
for key in myshelf.keys(): keylist.append(key)
Keep in mind that the “myshelf” is just a name for a dictionary or shelf and that the “keylist” is just a list holding the keys.

Shelve Practice 2

Use the attached database. It should be called mydb.db . Using it, first create a total variable. Then, add all of the shelve’s values to the total. Remember to check if the value is an integer before adding it to the total. After all, shelves can store all types. It should add up to 9999.

Shelve Practice 3

Create a database to store orders. Next, ask the customer for their name and store that as a variable. Next, ask the customer whether they want to view their previous order or make a new order. If they want to make a new order, use the shelf to store the order as the value and the customer’s name as the key. If they want to view a previous order, check if their name is in the shelf’s keys. If it is, print their previous order. If not, tell them that they haven’t ordered. Remember to close the shelf. Hint: to store their order, you can do: shelf[name] = order

Previous Section

20.4 Shelve

Next Section

20.6 JSON
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.