Python_List_Demonstration
This notebook demonstrates my understanding of lists in python
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)
list = ["1", "2", "3", "4", "5", "6"]
print('Regular List:', list)
list.reverse()
print('Reversed List:', list)
import random
print('Regular List:', list)
random.shuffle(list)
print('Random List:', list)
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()
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()
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)
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!")