InfoDb Cell

In the below cell, I create and add to a dictionaries to a list. Then at the end, I prompt the user to input their own information to add to the list. Then the list is printed.

InfoDb = []

# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
    "FirstName": "John",
    "LastName": "Mortensen",
    "DOB": "October 21",
    "Residence": "San Diego",
    "Email": "jmortensen@powayusd.com",
    "Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sunny",
    "LastName": "Naidu",
    "DOB": "August 2",
    "Residence": "Temecula",
    "Email": "snaidu@powayusd.com",
    "Owns_Cars": ["4Runner"]
})
# Append a 3rd dictionary
InfoDb.append({
    "FirstName": "Kaiden",
    "LastName": "Do",
    "DOB": "September 10",
    "Residence": "San Diego",
    "Email": "kaidend@powayusd.com",
    "Owns_Cars": ["Nissan Cube"]
})
# Append a 4th dictionary
InfoDb.append({
    "FirstName": "Samit",
    "LastName": "Poojary",
    "DOB": "February 24",
    "Residence": "San Diego",
    "Email": "samitp@powayusd.com",
    "Owns_Cars": ["Nissan Altima"]
})

import getpass, sys


FirstName = input("Enter your first name: ")
LastName = input("Enter your last name: ")
DOB = input("Enter your date of birth: ")
Residence = input("Enter your city: ")
Email = input("Enter your email: ")
Owns_Cars = input("Enter your car or dream car: ")

# Append a 5th dictionary with inputted values    
InfoDb.append({
    "FirstName": FirstName,
    "LastName": LastName,
    "DOB": DOB,
    "Residence": Residence,
    "Email": Email,
    "Owns_Cars": Owns_Cars
})

# Print the data structure
print(InfoDb)
[{'FirstName': 'John', 'LastName': 'Mortensen', 'DOB': 'October 21', 'Residence': 'San Diego', 'Email': 'jmortensen@powayusd.com', 'Owns_Cars': ['2015-Fusion', '2011-Ranger', '2003-Excursion', '1997-F350', '1969-Cadillac']}, {'FirstName': 'Sunny', 'LastName': 'Naidu', 'DOB': 'August 2', 'Residence': 'Temecula', 'Email': 'snaidu@powayusd.com', 'Owns_Cars': ['4Runner']}, {'FirstName': 'Kaiden', 'LastName': 'Do', 'DOB': 'September 10', 'Residence': 'San Diego', 'Email': 'kaidend@powayusd.com', 'Owns_Cars': ['Nissan Cube']}, {'FirstName': 'Samit', 'LastName': 'Poojary', 'DOB': 'February 24', 'Residence': 'San Diego', 'Email': 'samitp@powayusd.com', 'Owns_Cars': ['Nissan Altima']}, {'FirstName': 'Kaiden', 'LastName': 'Do', 'DOB': 'September 10', 'Residence': 'San Diego', 'Email': 'kaiden12345do@gmail.com', 'Owns_Cars': 'Nissan Cube'}]

Fruits Database

This database is a list of fruits with 6 dictionary values. The 6 fruits are apple, orange, pineapple, pear, blueberry, and grape. Each of the dictionaries consists of keys/values for the fruits' name, color, and size.

Fruits = []

# append apple dictionary
Fruits.append({
    "Name": "Apple",
    "Color": "Red",
    "Size": "Normal"
})

# append orange dictionary
Fruits.append({
    "Name": "Orange",
    "Color": "Orange",
    "Size": "Normal"
})

# append pineapple dictionary
Fruits.append({
    "Name": "Pineapple",
    "Color": "Yellow",
    "Size": "Large"
})

# append pear dictionary
Fruits.append({
    "Name": "Pear",
    "Color": "Green",
    "Size": "Normal"
})

# append blueberry dictionary
Fruits.append({
    "Name": "Blueberry",
    "Color": "Blue",
    "Size": "Small"
})

# append grape dictionary
Fruits.append({
    "Name": "Grape",
    "Color": "Purple",
    "Size": "Small"
})

# prints the whole list
print(Fruits)
[{'Name': 'Apple', 'Color': 'Red', 'Size': 'Normal'}, {'Name': 'Orange', 'Color': 'Orange', 'Size': 'Normal'}, {'Name': 'Pineapple', 'Color': 'Yellow', 'Size': 'Large'}, {'Name': 'Pear', 'Color': 'Green', 'Size': 'Normal'}, {'Name': 'Blueberry', 'Color': 'Blue', 'Size': 'Small'}, {'Name': 'Grape', 'Color': 'Purple', 'Size': 'Small'}]

This function is used to format the outputs of fruits when printing the fruits using some sort of loop.

def print_data(d_rec):
    print(d_rec["Name"])
    print("\t", "Color: ", d_rec["Color"])
    print("\t", "Size: ", d_rec["Size"])
    print()

For Loop

This for loop prints the fruits and their details in order.

def for_loop():
    print("For Loop\n")
    for record in Fruits: # loops for everything in Fruits
        print_data(record) # calls the print formatter

for_loop() # calls the function
For Loop

Apple
	 Color:  Red
	 Size:  Normal

Orange
	 Color:  Orange
	 Size:  Normal

Pineapple
	 Color:  Yellow
	 Size:  Large

Pear
	 Color:  Green
	 Size:  Normal

Blueberry
	 Color:  Blue
	 Size:  Small

Grape
	 Color:  Purple
	 Size:  Small

For Loop (reversed)

This for loop prints the fruits and their details in reverse order(from Grape to Apple).

def rev_for_loop():
    print("For Loop with Reversed Outputs\n")
    for record in reversed(Fruits):  # reverses the list
        print_data(record)
        
rev_for_loop() # calls the function
For Loop with Reversed Outputs

Grape
	 Color:  Purple
	 Size:  Small

Blueberry
	 Color:  Blue
	 Size:  Small

Pear
	 Color:  Green
	 Size:  Normal

Pineapple
	 Color:  Yellow
	 Size:  Large

Orange
	 Color:  Orange
	 Size:  Normal

Apple
	 Color:  Red
	 Size:  Normal

While Loop (shuffled)

This while loop prints the fruits that have been randomly shuffled.

import random
shuffledFruits = random.sample(Fruits, len(Fruits)) # randomly shuffles the list
def while_loop():
    print("While Loop\n")
    i = 0
    while i < len(shuffledFruits):  # while i is less than the length of the list, it loops
        record = shuffledFruits[i]
        print_data(record)
        i += 1  # adds 1 to i each loop
    return

while_loop() # calls the function
While Loop

Blueberry
	 Color:  Blue
	 Size:  Small

Orange
	 Color:  Orange
	 Size:  Normal

Apple
	 Color:  Red
	 Size:  Normal

Grape
	 Color:  Purple
	 Size:  Small

Pear
	 Color:  Green
	 Size:  Normal

Pineapple
	 Color:  Yellow
	 Size:  Large

Recursive Loop

This loop prints the fruits in order using a recursive loop.

def recursive_loop(x):  # keeps on looping if x is there
    if x < len(Fruits):  # if x is less than the length of the list, it will do the following
        # if x is greater it will stop
        record = Fruits[x]
        print_data(record)
        recursive_loop(x + 1) # makes it loop again
    return

# printing fruits in order using a recursive loop
print("Recursive Loop\n")
recursive_loop(0) # calls the function
Recursive Loop

Apple
	 Color:  Red
	 Size:  Normal

Orange
	 Color:  Orange
	 Size:  Normal

Pineapple
	 Color:  Yellow
	 Size:  Large

Pear
	 Color:  Green
	 Size:  Normal

Blueberry
	 Color:  Blue
	 Size:  Small

Grape
	 Color:  Purple
	 Size:  Small

Quiz that Stores Responses

This quiz asks the user 6 questions and saves each response. At the end, the quiz outputs the user's fraction and percentage of questions correct. Then the test outputs the user's answers.

questions = 6
correct = 0

def question_and_answer(prompt, answer): # function that has the prompt and answer as parameters
    print("Question: " + prompt) # displays the prompt
    msg = input()
    print("Answer: " + msg) # displays the user's answer
    
    if answer == msg.lower(): # compares the user's answer to correct answer, no regard to case
        print("Correct Answer") # if correct, prints that it is correct
        global correct
        correct += 1
    else:
        print ("Incorrect Answer") # if not, prints that it is incorrect
    return msg # returns the user's answer

# these add the user's answers to a list
Q1 = question_and_answer("What is 2+2?", "4")
Q2 = question_and_answer("What is the capital of California?", "sacramento")
Q3 = question_and_answer("What continent is India in?", "asia")
Q4 = question_and_answer("When is Christmas?", "december 25")
Q5 = question_and_answer("What number is Aaron Donald?", "99")
Q6 = question_and_answer("What color is the sky", "blue")

print(correct, "answers correct or", correct*100/questions,"%") # calculates the user's score
    
Quiz = {
    "Q1": Q1,
    "Q2": Q2,
    "Q3": Q3,
    "Q4": Q4,
    "Q5": Q5,
    "Q6": Q6
}

print("Your answers:",Quiz) # prints the user's answers
Question: What is 2+2?
Answer: 4
Correct Answer
Question: What is the capital of California?
Answer: SacramEnto
Correct Answer
Question: What continent is India in?
Answer: ASIA
Correct Answer
Question: When is Christmas?
Answer: december 25
Correct Answer
Question: What number is Aaron Donald?
Answer: 99
Correct Answer
Question: What color is the sky
Answer: Blue
Correct Answer
6 answers correct or 100.0 %
Your answers: {'Q1': '4', 'Q2': 'SacramEnto', 'Q3': 'ASIA', 'Q4': 'december 25', 'Q5': '99', 'Q6': 'Blue'}