For Loop Test

This algorithm loops the given block of code for a known number of cycles. In this case it is twice because we have appended two dictionaries to the list.

InfoDb = []
# Data structure with keys and values

# Append to list a Dictionary of key/values related to a person and their hobbies
InfoDb.append({
    "FirstName": "Aiden",
    "LastName": "Huynh",
    "DOB": "May 12",
    "Residence": "Escondido",
    "Email": "ah5993909@gmail.com",
    "Hobbies": ["Gaming", "Video Editing"]
})

# Append a 2nd dictionary
InfoDb.append({
    "FirstName": "Avinh",
    "LastName": "Huynh",
    "DOB": "December 27",
    "Residence": "Escondido",
    "Email": "avinhahuynh@gmail.com",
    "Hobbies": ["Gaming", "Streaming"]
})

# Prints all of the dictionary values
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"]) # comma adds space between two values
    print("\t", "Residence:", d_rec["Residence"]) # \t adds an indent
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Hobbies: ", end="")
    print(", ".join(d_rec["Hobbies"]))
    print()

# Loop algorithm
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)
        
for_loop()
For loop output

Aiden Huynh
	 Residence: Escondido
	 Birth Day: May 12
	 Hobbies: Gaming, Video Editing

Avinh Huynh
	 Residence: Escondido
	 Birth Day: December 27
	 Hobbies: Gaming, Streaming

While Loop Test

This algorithm only loops while a certain condition is met. In this case, it will only loop while the variable "i" is less than the number of items in the list given by the len command. The way we make this function is by adding one to "i" at the end of each loop, making it increase by one for each cycle until eventually becoming equal to the number of items in the list, thus ending the loop.

InfoDb = []
# Data structure with keys and values

# Append to list a Dictionary of key/values related to a person and their hobbies
InfoDb.append({
    "FirstName": "Aiden",
    "LastName": "Huynh",
    "DOB": "May 12",
    "Residence": "Escondido",
    "Email": "ah5993909@gmail.com",
    "Hobbies": ["Gaming", "Video Editing"]
})

# Append a 2nd dictionary
InfoDb.append({
    "FirstName": "Avinh",
    "LastName": "Huynh",
    "DOB": "December 27",
    "Residence": "Escondido",
    "Email": "avinhahuynh@gmail.com",
    "Hobbies": ["Gaming", "Streaming"]
})

# Prints all of the dictionary values
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"]) # comma adds space between two values
    print("\t", "Residence:", d_rec["Residence"]) # \t adds an indent
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Hobbies: ", end="")
    print(", ".join(d_rec["Hobbies"]))
    print()
    
# While Loop -> While a specified condition is met (i < len(InfoDb)), it will 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

Aiden Huynh
	 Residence: Escondido
	 Birth Day: May 12
	 Hobbies: Gaming, Video Editing

Avinh Huynh
	 Residence: Escondido
	 Birth Day: December 27
	 Hobbies: Gaming, Streaming

Recursive Loop Test

Functions pretty much the same as the above while loop, except it runs itself within the loop to add 1 to i.

InfoDb = []
# Data structure with keys and values

# Append to list a Dictionary of key/values related to a person and their hobbies
InfoDb.append({
    "FirstName": "Aiden",
    "LastName": "Huynh",
    "DOB": "May 12",
    "Residence": "Escondido",
    "Email": "ah5993909@gmail.com",
    "Hobbies": ["Gaming", "Video Editing"]
})

# Append a 2nd dictionary
InfoDb.append({
    "FirstName": "Avinh",
    "LastName": "Huynh",
    "DOB": "December 27",
    "Residence": "Escondido",
    "Email": "avinhahuynh@gmail.com",
    "Hobbies": ["Gaming", "Streaming"]
})

# Prints all of the dictionary values
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"]) # comma adds space between two values
    print("\t", "Residence:", d_rec["Residence"]) # \t adds an indent
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Hobbies: ", end="")
    print(", ".join(d_rec["Hobbies"]))
    print()
    
# Recursive Loop -> keeps incrementing on each call (n+1) until exit condition is met
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

Aiden Huynh
	 Residence: Escondido
	 Birth Day: May 12
	 Hobbies: Gaming, Video Editing

Avinh Huynh
	 Residence: Escondido
	 Birth Day: December 27
	 Hobbies: Gaming, Streaming

Reversed List

GList = [
    ["Apple", "1.32"],
    ["Orange", "1.45"],
    ["Banana", "0.62"],
    ["Strawberry", "0.11"],
    ["Blueberry", "0.02"],
    ["Pineapple", "2.18"],
    ["Watermelon", "4.98"],
    ["Kiwi", "1.07"],
    ["Mango", "1.32"],
    ["Grape", "0.03"]
]

# Reverses the list
GList.reverse()

print(GList)
[['Grape', '0.03'], ['Mango', '1.32'], ['Kiwi', '1.07'], ['Watermelon', '4.98'], ['Pineapple', '2.18'], ['Blueberry', '0.02'], ['Strawberry', '0.11'], ['Banana', '0.62'], ['Orange', '1.45'], ['Apple', '1.32']]

Randomized List

import random
# Imports various commands for randomization

# List of fruits and their prices
GList = [
    ["Apple", "1.32"],
    ["Orange", "1.45"],
    ["Banana", "0.62"],
    ["Strawberry", "0.11"],
    ["Blueberry", "0.02"],
    ["Pineapple", "2.18"],
    ["Watermelon", "4.98"],
    ["Kiwi", "1.07"],
    ["Mango", "1.32"],
    ["Grape", "0.03"]
]

# Randomizes (shuffles) the list
random.shuffle(GList)

print(GList)
[['Strawberry', '0.11'], ['Mango', '1.32'], ['Orange', '1.45'], ['Watermelon', '4.98'], ['Grape', '0.03'], ['Banana', '0.62'], ['Blueberry', '0.02'], ['Kiwi', '1.07'], ['Pineapple', '2.18'], ['Apple', '1.32']]

Random Grocery List Generator

Uses lists and a loop to generate a specified number of fruits and their respective prices, along with the sum of all of the prices.

import random

# List of fruits and their prices
GList = [
    ["Apple", "1.32"],
    ["Orange", "1.45"],
    ["Banana", "0.62"],
    ["Strawberry", "0.11"],
    ["Blueberry", "0.02"],
    ["Pineapple", "2.18"],
    ["Watermelon", "4.98"],
    ["Kiwi", "1.07"],
    ["Mango", "1.32"],
    ["Grape", "0.03"]
]

print("Input the desired number of fruits (esc to cancel):")
fruitCount = input() # Prompts user on the desired amount of fruits

# k defines the number of fruits to pull, and by setting that equal to the input(), we pull only the amount desired by the user
selectedPairs = random.choices(GList, k=int(fruitCount))
# int() is used because k must be an integer, did not do this in line 18 because input() must be a string to be printed

print("You selected "+fruitCount+" fruits:")

# This section defines the first term as the fruit and second as the corresponding price, allowing us to use each part separately
for pricePair in selectedPairs:
    
    fruit = pricePair[0]
    price = pricePair[1]
    
    print(fruit+": $"+price)
    
    # Calculates the sum of the prices
    n = float(price)
    sum = sum+n
    total = round(sum, 2)

print("Your total is: $"+str(total))

sum = 0
Input the desired number of fruits (esc to cancel):
You selected 10 fruits:
Strawberry: $0.11
Apple: $1.32
Strawberry: $0.11
Blueberry: $0.02
Grape: $0.03
Apple: $1.32
Pineapple: $2.18
Strawberry: $0.11
Mango: $1.32
Banana: $0.62
Your total is: $7.14

Quiz using lists

Basic Python Quiz