InfoDb Database background
The InfoDb database is a python dictionary, structured with values that are associated with keys. This makes it simple to extract specific information from a dictionary, based on its key. You can use database.append to add to the database.
Here, information is manually input into a dictionary called InfoDb using database.append.
InfoDb = [] #defining InfoDb as a dictionary
InfoDb.append({
"FirstName": "Jaden", #"key": "value"
"LastName": "Nguyen",
"FavFood": "March 16",
"FavColor": "Blue",
"CellPhone": "8589148661",
"Age": "15",
"Siblings": ["Brian", "Katelyn"]
})
print(InfoDb)
Here, I created functions collect_data and more_info to add to the database using input.
more_info is a while loop. As long as the input to "Do you want to enter information?" is "Yes", the function will prompt the user to input information, then ask again. If it is any other answer, the loop ends.
I also made a function print_db to print the contents of the database neatly. The output contains a second person's information that was added using collect_data and more_info.
def collect_data():
fn = input("What is your first name? ") #storing inputs in variables
ln = input("What is your last name? ")
ff = input("What is your favorite food? ")
fc = input("What is your favorite color? ")
cell = input("What is your phone number? ")
age = input("What is your age? ")
sibs = input("What are your siblings' names, from oldest to youngest? ")
InfoDb.append({
"FirstName": fn, #"key": "value" but with variables
"LastName": ln,
"FavFood": ff,
"FavColor": fc,
"CellPhone": cell,
"Age": age,
"Siblings": [sibs]
})
def more_info():
while True:
ans = input("Do you want to add more info to the database? ")
if ans == "yes":
name = True
collect_data() #collect data if input is "yes"
print()
else: #stop function if input isn't "yes"
break
more_info()
def print_db(db):
print("Here is the information for " + db["FirstName"], db["LastName"] + ":") #get "value" using database["key"]
print()
print("Favorite Food: " + db["FavFood"])
print("Favorite Color: " + db["FavColor"])
print("Phone Number: " + db["CellPhone"])
print("Age: " + db["Age"])
print("Siblings: " + ", ".join(db["Siblings"]))
print()
print()
Here, more_recursion is a function that has the same purpose as more_info, but uses recursion instead of a while loop. When the input is "yes", the function is repeated. When it is anything else, the function ends.
def more_recursion():
ans = input("Do you want to add more info to the database? ")
if ans == "yes":
name = True
collect_data()
print()
more_recursion() #function inside of the same function is recursion
else:
return
more_recursion()
Here is a for loop. It says: for each person in the database, print the person's information(using the print_db function).
for person in InfoDb: #for each "person" in teh database, print the person's information
print_db(person)
Here, I used a for loop with index to list the letters of each person's name in order.
def let_sort(sub):
name = sub["FirstName"]
for let in name: # for each letter in the person's name
place = (name.index(let) + 1) #"place" is the letter's spot, +1 because index starts at 0
print("Letter number " + str(place) + " in " + name + " is " + let)
for person in InfoDb:
let_sort(person)
print()