Defining InfoDb with extra entries

Purpose of this cell is to define InfoDb and add entries about certain people and traits that they have

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 loop to print out InfoDb

Purpose of this independent cell is to use a normal loop that goes through Infodb printint out all the entries. The for loop automates the process so we do not have to manually print out all the enries by hand.

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"])
samit poojary 
 	 DOB: September 15 
 	 Residence: San Diego 
 	 Email:  samit.poojary@gmail.com 
 	 Phone:  iphone
Safin Singh 
 	 DOB: July 13 
 	 Residence: San Diego 
 	 Email:  safin.singh@gmail.com 
 	 Phone:  android
Alex Kumar 
 	 DOB: May 9 
 	 Residence: San Diego 
 	 Email:  alex.k.kumar@gmail.com 
 	 Phone:  iphone
Maxie Kumar 
 	 DOB: November 5 
 	 Residence: San Diego 
 	 Email:  maxie.kumar@gmail.com 
 	 Phone:  no phone

While loop to print out InfoDb

The purpose of this independent cell block is use a while loop to go print out InfoDb while a certain variable is less than the length of InfoDb. This allows us to print out the data without manually inputting the values to the console

#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
samit poojary 
 	 DOB: September 15 
 	 Residence: San Diego 
 	 Email:  samit.poojary@gmail.com 
 	 Phone:  iphone
Safin Singh 
 	 DOB: July 13 
 	 Residence: San Diego 
 	 Email:  safin.singh@gmail.com 
 	 Phone:  android
Alex Kumar 
 	 DOB: May 9 
 	 Residence: San Diego 
 	 Email:  alex.k.kumar@gmail.com 
 	 Phone:  iphone
Maxie Kumar 
 	 DOB: November 5 
 	 Residence: San Diego 
 	 Email:  maxie.kumar@gmail.com 
 	 Phone:  no phone

for loop with index to print out InfoDb

The purpose of this independent code cell is to use a for loop with an index that will go over all the data in infoDB without having to manually print out every single statement. The for loop will repeat the print function for every single entry in InfoDb

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"])
samit poojary 
 	 DOB: September 15 
 	 Residence: San Diego 
 	 Email:  samit.poojary@gmail.com 
 	 Phone:  iphone
Safin Singh 
 	 DOB: July 13 
 	 Residence: San Diego 
 	 Email:  safin.singh@gmail.com 
 	 Phone:  android
Alex Kumar 
 	 DOB: May 9 
 	 Residence: San Diego 
 	 Email:  alex.k.kumar@gmail.com 
 	 Phone:  iphone
Maxie Kumar 
 	 DOB: November 5 
 	 Residence: San Diego 
 	 Email:  maxie.kumar@gmail.com 
 	 Phone:  no 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)
samit poojary
     DOB: September 15
     Residence: San Diego
     Email: samit.poojary@gmail.com
     Phone: iphone
    
Safin Singh
     DOB: July 13
     Residence: San Diego
     Email: safin.singh@gmail.com
     Phone: android
    
Alex Kumar
     DOB: May 9
     Residence: San Diego
     Email: alex.k.kumar@gmail.com
     Phone: iphone
    
Maxie Kumar
     DOB: November 5
     Residence: San Diego
     Email: maxie.kumar@gmail.com
     Phone: no phone
    

Outputting data in Reverse Order

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])
['Kyrie Irving', 'Paul George', 'Jason Tatum', 'Kevin Durant', 'Chris Paul', 'Kobe Bryant', 'Lebron James']

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
Question: Whats the largest mammal
blue whale is correct
Question: whats the fastest animal
cheetah is correct
Question: what color is a flamingo
pink is correct
Question: Whats is the tallest animal
giraffe is correct
100.0 %