InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "samit",
"LastName": "poojary",
"DOB": "September 15",
"Residence": "San Diego",
"Email": "samit.poojary@gmail.com",
"Phone": "iphone"
})
#adding extra records to InfoDb
InfoDb.append({
"FirstName": "Safin",
"LastName": "Singh",
"DOB": "July 13",
"Residence": "San Diego",
"Email": "safin.singh@gmail.com",
"Phone": "android"
})
# adding extra records
InfoDb.append({
"FirstName": "Alex",
"LastName": "Kumar",
"DOB": "May 9",
"Residence": "San Diego",
"Email": "alex.k.kumar@gmail.com",
"Phone": "iphone"
})
# Adding one extra item to InfoDb
InfoDb.append({
"FirstName": "Maxie",
"LastName": "Kumar",
"DOB": "November 5",
"Residence": "San Diego",
"Email": "maxie.kumar@gmail.com",
"Phone": "no phone"
})
for item in InfoDb:
#print function that goes through and formats
print(item["FirstName"], item["LastName"], "\n", "\t", "DOB:", item["DOB"], "\n", "\t", "Residence:", item["Residence"], "\n", "\t", "Email: ", item["Email"], "\n", "\t", "Phone: " ,item["Phone"])
#Sets number to the length of InfoDb
number = len(InfoDb)
#temporary variable i set to 0
i = 0
while i < number:
#while i is less than the number or the length of InfoDb, it prints out the data in InfoDb
print(InfoDb[i]["FirstName"], InfoDb[i]["LastName"], "\n", "\t", "DOB:", InfoDb[i]["DOB"], "\n", "\t", "Residence:", InfoDb[i]["Residence"], "\n", "\t", "Email: ", InfoDb[i]["Email"], "\n", "\t", "Phone: " ,InfoDb[i]["Phone"])
i+=1
for i in range(len(InfoDb)):
#prints out the items for every i in the InfoDb
print(InfoDb[i]["FirstName"], InfoDb[i]["LastName"], "\n", "\t", "DOB:", InfoDb[i]["DOB"], "\n", "\t", "Residence:", InfoDb[i]["Residence"], "\n", "\t", "Email: ", InfoDb[i]["Email"], "\n", "\t", "Phone: " ,InfoDb[i]["Phone"])
Recursion to print out all of InfoDb
This recursion which utilizes a function to print out all the data in InfoDb is used to automate the printint process so we will not have to manually print out every single line. It uses a function that also uses embedded if statements that also calls itself at the end. This allows us to go over every single entry inside of InfoDb.
#sets variable n equal to the length of Info Db
n = len(InfoDb)
#function that checks to see if i is n, and if it is non, it will parse through InfoDb and print it out
def output(i):
if i == n: return
#format string to output InfoDb in an organized foramt
print("""{} {}
DOB: {}
Residence: {}
Email: {}
Phone: {}
""".format(InfoDb[i]["FirstName"], InfoDb[i]["LastName"], InfoDb[i]["DOB"], InfoDb[i]["Residence"], InfoDb[i]["Email"], InfoDb[i]["Phone"]))
#recalls the function output to go over every single person in InfoDb
output(1+i)
output(0)
Names_of_basketball_players = ["Lebron James", "Kobe Bryant", "Chris Paul", "Kevin Durant", "Jason Tatum", "Paul George", "Kyrie Irving"]
#prints the list out in reverse order
print(Names_of_basketball_players[::-1])
Quiz That stores into a dictionary
The purpose of this code block is to test for basic animal trivia and it does this through multiple functions that stores the answers and questions in a dictionary. It calls back to the dictionary using for loops and then embedded whithin the for loops, it calls back to the function to print out the question and take input.
def question_and_answers(input): #function that prints out the question and takes in input
print("Question: " + input)
correct = 0 #percent correct variable
#dictionary with answers
questions_answers = [{"Whats the largest mammal" : "blue whale",
"whats the fastest animal" : "cheetah",
"what color is a flamingo" : "pink",
"Whats is the tallest animal" : "giraffe"}]
for i in questions_answers:
for question, answer in i.items():
question_and_answers(question) # calls back to the questionanswer function
response = input("Answer: ") # variable that connects to the user's input
if response.lower() == answer: # if the answer provided is correct
print(response, "is correct")
correct += 1
else: # if the answer provided is wrong
print(response, "is wrong")
print(correct/4*100, "%") #outputs the correct variable as a percentage