List of All Hacks

  1. Add a couple of records to the InfoDb
  2. Try to do a for loop with an index
  3. Would it be possible to output data in a reverse order?
  4. Are there other methods that can be performed on lists?
  5. Could you create new or add to dictionary data set? Could you do it with input?
  6. Make a quiz that stores in a List of Dictionaries.

Hack #1: Add records to the InfoDb

Here is my InfoDb where I added two new records to it.

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 to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sophia",
    "LastName": "Tang",
    "DOB": "Septembr 4",
    "Residence": "San Diego",
    "Email": "sophiat14470@powayusd.com",
    "Owns_Cars": ["None"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Random",
    "LastName": "Person",
    "DOB": "January 1",
    "Residence": "New York City",
    "Email": "randomemail@domain.com",
    "Owns_Cars": ["Tesla"]
})

# 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': 'Sophia', 'LastName': 'Tang', 'DOB': 'Septembr 4', 'Residence': 'San Diego', 'Email': 'sophiat14470@powayusd.com', 'Owns_Cars': ['None']}, {'FirstName': 'Random', 'LastName': 'Person', 'DOB': 'January 1', 'Residence': 'New York City', 'Email': 'randomemail@domain.com', 'Owns_Cars': ['Tesla']}]

Define the printing data function (prints the data from the input)

def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"])  # using comma puts space between values
    print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Cars: ", end="")  # end="" make sure no return occurs
    print(", ".join(d_rec["Owns_Cars"]))  # join allows printing a string list with separator
    print()

Hack #2: For loop with an index

For loop with an index to print all the InfoDb

for record in range(0, len(InfoDb)):
    print_data(InfoDb[record])
John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Sophia Tang
	 Residence: San Diego
	 Birth Day: Septembr 4
	 Cars: None

Random Person
	 Residence: New York City
	 Birth Day: January 1
	 Cars: Tesla

Hack #3: Output data in reverse order

I used the reversed build-in python function

for record in reversed(range(0, len(InfoDb))):
    print_data(InfoDb[record])
Random Person
	 Residence: New York City
	 Birth Day: January 1
	 Cars: Tesla

Sophia Tang
	 Residence: San Diego
	 Birth Day: Septembr 4
	 Cars: None

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Hack #4: Other Methods with Lists

Find greatest and least number in a list

numList = [4, 7, 2, 67, 45, 90, 21, 34, 65, 31]

# Find the max of the list

greatest = 0
for i in range(0, 10):
    if (numList[i] > greatest):
        greatest = numList[i]
print("The greatest in this list is " + str(greatest))

least = 199999
for i in range(0, 10):
    if (numList[i] < least):
        least = numList[i]
print("The least in this list is " + str(least))
The greatest in this list is 90
The least in this list is 2

Hack #5: Get Dictionary Values with Input

It asks you how many records you want to input and even allows you to put in a list of cars!

newDb = []

num = input("How many records do you want to enter in?")

def getInput():
    inputQuestions = ["Enter in a first name: ", "Enter in a last name: ", "Enter in a DOB: ", "Enter in a residence: ", "Enter in an email: ", "How many cars does this person have?", "Enter in owned car: "]
    inputList = []
    for i in range(0, 5):
        temp = input(inputQuestions[i])
        inputList.append(temp)
    cars = int(input(inputQuestions[5]))
    for i in range (0, cars):
        temp = input(inputQuestions[6])
        inputList.append(temp)
    return(inputList)

for i in range(0, int(num)):
    carsList = []
    add = getInput()
    for j in range (5, len(add)):
        carsList.append(add[j])
    newDb.append({
        "FirstName": add[0],
        "LastName": add[1],
        "DOB": add[2],
        "Residence": add[3],
        "Email": add[4],
        "Owns_Cars": carsList
    })

print(newDb)
[{'FirstName': 'Sophia', 'LastName': 'Tang', 'DOB': '9/4', 'Residence': 'San Diego', 'Email': 'email@email.com', 'Owns_Cars': ['car1', 'car2']}]

Hack #6: Quiz Using Dictionaries

I made a quiz about myself using python dictionaries!

import getpass, sys

def question(prompt, answer):
    msg = input("Question: " + prompt)
    if msg == answer:
        print(msg + " is correct!")
        return(1)
    else:
        print(msg + " is incorrect!")
        return(0)

print("Welcome to a quiz about me :)")


# Append to List a Dictionary of key/values related to a person and cars
quiz = {
    "1. What is my name?": "Sophia",
    "2. What is my favorite food (one word)?": "noodles",
    "3. What month am I born in?": "September",
    "4. What school do I go to (four letters, all caps)?": "DNHS",
}

numqs = 4
correct = 0

for item in quiz:
    num = question(item, quiz[item])
    correct = correct + num

print("You scored" + str(correct) +"/" + str(numqs))
print("Your percentage is " + str(correct/numqs*100) + "%! Good work!")
Welcome to a quiz about me :)
Sophia is correct!
noodles is correct!
September is correct!
DNHS is correct!
You scored4/4
Your percentage is 100.0%! Good work!

Using While 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

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Sophia Tang
	 Residence: San Diego
	 Birth Day: Septembr 4
	 Cars: None

Random Person
	 Residence: New York City
	 Birth Day: January 1
	 Cars: Tesla

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

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Sophia Tang
	 Residence: San Diego
	 Birth Day: Septembr 4
	 Cars: None

Random Person
	 Residence: New York City
	 Birth Day: January 1
	 Cars: Tesla

Food Dictionary

Here is an extra dictionary I made about foods that I like :)

food = []

# create print_data function for food
def print_food(d_rec):
    print(d_rec["FoodName"])
    print("\t", "Cuisine:", d_rec["Cuisine"])
    print("\t", "Taste:", d_rec["Taste"])
    print("\t", "Ingredients: ", end="")
    print(", ".join(d_rec["Ingredients"]))
    print("\t", "Preparation Time:", d_rec["Preparation Time"])
    print("\t", "Cook Time:", d_rec["Cooking Time"])
    print("\t", "Rating:", d_rec["Rating"])
    print()

# append information about different foods in the form of dictionaries
food.append({
    "FoodName": "Soup Noodles",
    "Cuisine": "Chinese",
    "Taste": "Light and savory",
    "Ingredients": ["Noodles", "water", "bok choy", "meats", "vegetables", "poached egg", "tomato"],
    "Preparation Time": "20 minutes",
    "Cooking Time": "15 minutes",
    "Rating": "9/10",
})

food.append({
    "FoodName": "Sushi",
    "Cuisine": "Japanese",
    "Taste": "Light and savory",
    "Ingredients": ["Rice", "seaweed", "salmon", "avocado", "ingredients as you wish"],
    "Preparation Time": "30 minutes",
    "Cooking Time": "15 minutes",
    "Rating": "9/10",
})

food.append({
    "FoodName": "Pizza",
    "Cuisine": "Italian",
    "Taste": "Savory/cheesy",
    "Ingredients": ["Tomato sauce", "dough", "pepperoni", "pineapple", "other toppings"],
    "Preparation Time": "30 minutes",
    "Cooking Time": "1 hour",
    "Rating": "7/10",
})

food.append({
    "FoodName": "Fried rice",
    "Cuisine": "Chinese",
    "Taste": "Savory",
    "Ingredients": ["Leftover rice", "eggs", "peas", "carrots", "other veggies/meats"],
    "Preparation Time": "10 minutes",
    "Cooking Time": "10 minutes",
    "Rating": "8/10",
})

for record in range(0, len(food)):
    print_food(food[record])
Soup Noodles
	 Cuisine: Chinese
	 Taste: Light and savory
	 Ingredients: Noodles, water, bok choy, meats, vegetables, poached egg, tomato
	 Preparation Time: 20 minutes
	 Cook Time: 15 minutes
	 Rating: 9/10

Sushi
	 Cuisine: Japanese
	 Taste: Light and savory
	 Ingredients: Rice, seaweed, salmon, avocado, ingredients as you wish
	 Preparation Time: 30 minutes
	 Cook Time: 15 minutes
	 Rating: 9/10

Pizza
	 Cuisine: Italian
	 Taste: Savory/cheesy
	 Ingredients: Tomato sauce, dough, pepperoni, pineapple, other toppings
	 Preparation Time: 30 minutes
	 Cook Time: 1 hour
	 Rating: 7/10

Fried rice
	 Cuisine: Chinese
	 Taste: Savory
	 Ingredients: Leftover rice, eggs, peas, carrots, other veggies/meats
	 Preparation Time: 10 minutes
	 Cook Time: 10 minutes
	 Rating: 8/10

To Do List Using Dictionary and Input

This code asks you to input your todo list to help you keep track of what you need to do! All you need to specify in the beginning is how many items on your list and then you just enter in the necessary information about your todo list.

ToDoList = []

print("Welcome to a todo list to keep track of what you need to do!")
num = input("How many todo items do you need to enter in?")

def getInput():
    inputQuestions = ["Task name: ", "Category (Ex: home, school): ", "Estimated Time: ", "Due Date: ", "Urgency Value (1 for least, 10 for most): "]
    inputList = []
    for i in range(0, 5):
        temp = input(inputQuestions[i])
        inputList.append(temp)
    return(inputList)

for i in range(0, int(num)):
    add = getInput()
    ToDoList.append({
        "Task Name": add[0],
        "Category": add[1],
        "Estimated Time": add[2],
        "Due Date": add[3],
        "Urgency": add[4],
    })


# create print_data function for todo list
def print_tasks(d_rec):
    print(d_rec["Task Name"])
    print("\t", "Category:", d_rec["Category"])
    print("\t", "Estimated Time:", d_rec["Estimated Time"])
    print("\t", "Due Date:", d_rec["Due Date"])
    print("\t", "Urgency:", d_rec["Urgency"])
    print()

for record in range(0, len(ToDoList)):
    print_tasks(ToDoList[record])
Welcome to a todo list to keep track of what you need to do!
Humanities Beautiful Friday Presentation Slideshow
	 Category: school
	 Estimated Time: 1 hour
	 Due Date: 9/30
	 Urgency: 3

Read Lovely War
	 Category: school
	 Estimated Time: 2 hours
	 Due Date: 10/14
	 Urgency: 3

Laundry
	 Category: home
	 Estimated Time: 20 minutes
	 Due Date: Saturday
	 Urgency: 4

AP Calc HW
	 Category: school
	 Estimated Time: 30 minutes
	 Due Date: 9/7
	 Urgency: 6

AP Chem Lab Writeup
	 Category: school
	 Estimated Time: 2-3 hours
	 Due Date: 9/6
	 Urgency: 7