Computer Science
Grade 5
20 min
Adding Records: Populating Your Database with Information
Learn to add new records to the database table, filling in the values for each field.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Use the input() function to gather information from a user.
Create a dictionary to store a single record.
Use the append() method to add a new record (dictionary) to a list (database).
Construct a 'while' loop to add multiple records until a user decides to stop.
Construct a 'for' loop to add a specific number of records.
Explain the difference between a database, a record, and a field in the context of a Python list of dictionaries.
Have you ever wondered how a game saves all your characters or how a phone stores all your contacts? 📱 Let's learn how to build our own simple address book!
In this lesson, we will learn how to add new information, called 'records', to a list that acts as our database. We will use Python&#...
2
Key Concepts & Vocabulary
TermDefinitionExample
Database (in our case, a List)An organized collection of information. For us, our database will be a Python list that holds all of our records.A list named `my_pets` that holds information about many different pets. `my_pets = []`
Record (a Dictionary)A single entry or item in a database. We will use a Python dictionary to store one complete record.A single pet's information, like its name and type. `{'name': 'Fido', 'animal': 'Dog'}`
Field (a Key-Value Pair)A single piece of information within a record. In a dictionary, this is a key and its value.The pet's name is 'Fido'. The key is `'name'` and the value is `'Fido'`.
input() functionA command that lets us ask the user a question and ge...
3
Core Syntax & Patterns
The 'while' Loop Pattern for Adding Records
while condition:
# 1. Get input from user for each field
# 2. Create a dictionary (record) from the input
# 3. Append the dictionary to the list (database)
# 4. Ask user if they want to continue to update the condition
Use this pattern when you want to allow the user to add as many records as they want. The loop continues until the user types something to make the condition false (like typing 'no').
Creating a Record from Variables
variable1 = input('Prompt 1: ')
variable2 = input('Prompt 2: ')
new_record = {'key1': variable1, 'key2': variable2}
This is the standard way to build a record. First, you get all the pieces of information and store them in variables. The...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You need to write a program that asks for student names and their grades. It should keep asking until the user enters 'done' for the name. For each student, it should only add them to the `honor_roll` list if their grade is 90 or higher. Which combination of control structures is best?
A.`while` loop to keep asking, with an `if` statement inside to check the grade.
B.single `for` loop to handle everything.
C.An `if` statement to ask for the name, and a `while` loop inside to check the grade.
D.`for` loop to get the names and another `for` loop to check the grades.
Challenging
A program adds records of runners and their times. The loop must stop if the user enters 'stop' OR if 10 runners have been added. Which `while` condition correctly implements this logic? (Assume `count` starts at 0).
A.while name == 'stop' and count < 10:
B.while name != 'stop' and count < 10:
C.while name != 'stop' or count < 10:
D.while count > 10:
Challenging
A record for a pet is stored as a dictionary, like `{'name': 'Buddy', 'type': 'Dog'}`. How would you add a new pet record to a list called `pets` after getting input from the user?
A.pets.append(name, type)
B.pets.append('name': name, 'type': type)
C.new_pet = {'name': name, 'type': type}; pets.append(new_pet)
D.pets.append({name, type})
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free