InfoDb

Arrays or lists are very useful for storing information in an organized format. InfoDb is an array with information like names, date of birth, residence, cars, favorite shows, and favorite games. ".append" is adding the information in parentheses to InfoDb.

InfoDb = []

# To add data to InfoDB we must use the .append function
# A dictionary is made with curly braces {}
InfoDb.append({
    "FirstName": "Joshua",
    "MiddleName": "Benjamin",
    "LastName": "Williams",
    "DOB": "January 6",
    "Residence": "San Diego",
    "Owns_Cars": "2011 Nissan Versa",
    "Show": "SpongeBob",
    "Game": "Apex Legends"
})


InfoDb.append({
    "FirstName": "Ryan",
    "MiddleName": "Robert",
    "LastName": "McWeeny",
    "DOB": "March 27",
    "Residence": "San Diego",
    "Email": "ryanrob327@gmail.com",
    "Owns_Cars": "2016 GMC Acadia",
    "Show": "SpongeBob",
    "Game": "Minecraft"
})

# Print the data structure
print(InfoDb)
[{'FirstName': 'Joshua', 'MiddleName': 'Benjamin', 'LastName': 'Williams', 'DOB': 'January 6', 'Residence': 'San Diego', 'Owns_Cars': '2011 Nissan Versa', 'Show': 'SpongeBob', 'Game': 'Apex Legends'}, {'FirstName': 'Ryan', 'MiddleName': 'Robert', 'LastName': 'McWeeny', 'DOB': 'March 27', 'Residence': 'San Diego', 'Email': 'ryanrob327@gmail.com', 'Owns_Cars': '2016 GMC Acadia', 'Show': 'SpongeBob', 'Game': 'Minecraft'}]

Reversed list

This block of code flips the order of a list and prints the flipped list using ".reverse()" a function built into python that reverses the order of a list.

list = ["1", "2", "3", "4", "5", "6"]
print('Regular List:', list)
list.reverse()
print('Reversed List:', list)
Regular List: ['1', '2', '3', '4', '5', '6']
Reversed List: ['6', '5', '4', '3', '2', '1']

Random list

This block of code randomizes the order of a list and prints the randomized list using "random.shuffle()" a function from the random module built into python.

import random

print('Regular List:', list)
random.shuffle(list)
print('Random List:', list)
Regular List: ['1', '2', '3', '4', '5', '6']
Random List: ['3', '5', '1', '2', '4', '6']

For Loop

This is an example of printing the data from infoDb using a for loop, it repeats as many times as there are indices in InfoDb printing the result every loop.

def print_data(d_rec): # defines function that prints data
    print(d_rec["FirstName"], d_rec["MiddleName"], d_rec["LastName"])  # prints data from the dictionary
    print("\t", "Residence:", d_rec["Residence"]) 
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Cars: ", d_rec["Owns_Cars"])  # end="" make sure no return occurs
    print("\t", "Favorite show: ", d_rec["Show"])
    print("\t", "Favorite game: ", d_rec["Game"])


# for loop iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
For loop output

Joshua Benjamin Williams
	 Residence: San Diego
	 Birth Day: January 6
	 Cars:  2011 Nissan Versa
	 Favorite show:  SpongeBob
	 Favorite game:  Apex Legends
Ryan Robert McWeeny
	 Residence: San Diego
	 Birth Day: March 27
	 Cars:  2016 GMC Acadia
	 Favorite show:  SpongeBob
	 Favorite game:  Minecraft

While loop

This block of code prints data from infoDb using a while loop, while i is less than the length of InfoDb i iterates through InfoDb printing the result every loop.

def while_loop():
    print("While loop output\n")
    i = 0
    while i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        i += 1
    return

while_loop()
While loop output

Joshua Benjamin Williams
	 Residence: San Diego
	 Birth Day: January 6
	 Cars:  2011 Nissan Versa
	 Favorite show:  SpongeBob
	 Favorite game:  Apex Legends
Ryan Robert McWeeny
	 Residence: San Diego
	 Birth Day: March 27
	 Cars:  2016 GMC Acadia
	 Favorite show:  SpongeBob
	 Favorite game:  Minecraft

Recersive Loop

This is a block of code that uses a repeating function to iterate through InfoDb and print the result. This function always calls itself after executing so that it can execute again, this process is called recursion.

def recursive_loop(i):
    if i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        recursive_loop(i + 1)
    return
    
print("Recursive loop output\n")
recursive_loop(0)
Recursive loop output

Joshua Benjamin Williams
	 Residence: San Diego
	 Birth Day: January 6
	 Cars:  2011 Nissan Versa
	 Favorite show:  SpongeBob
	 Favorite game:  Apex Legends
Ryan Robert McWeeny
	 Residence: San Diego
	 Birth Day: March 27
	 Cars:  2016 GMC Acadia
	 Favorite show:  SpongeBob
	 Favorite game:  Minecraft

Quiz

This is a quiz made using dictionaries and lists, it is made so that there is no repeating code. This quiz keeps track of your score every time you enter an answer and tells you if it is write or wrong.

question = [] # creates empty list

question.append({ # adds dictionary with question and answer to list using .append
    "Question": "what is the fastest plane in the world",
    "Answer": "SR-71",
})
question.append({
    "Question": "what is the hightest flying plane in the world",
    "Answer": "X-15",
})
question.append({
    "Question": "what is the largest plane in the world",
    "Answer": "An-225",
})
question.append({
    "Question": "what is the smallest plane in the world",
    "Answer": "BD-5J",
})

points = 0
print("Take this quiz about planes.")

for i in question: # for loop repeats every time an answer is given
    print(i["Question"])
    response = input(i["Question"])
    print(response)
    
    if response == i["Answer"]:
        points += 1
        print("Correct, you have ", points, " points!")
    else:
        print("Incorrect, the answer was; ", i["Answer"])

print("You have finished the quiz with ", points, " out of ", len(question), " points!")
Take this quiz about planes.
what is the fastest plane in the world
SR-71
Correct, you have  1  points!
what is the hightest flying plane in the world
X-15
Correct, you have  2  points!
what is the largest plane in the world
U-2
Incorrect, the answer was;  An-225
what is the smallest plane in the world
BD-5J
Correct, you have  3  points!
You have finished the quiz with  3  out of  4  points!