Python List
The Python lists assignment. A large portion of this page is all for practice, press on the link in the table of contents to go directly to the quiz.
# variable of type string
name = "John Doe"
print("name", name, type(name))
# variable of type integer
age = 18
print("age", age, type(age))
# variable of type float
score = 90.0
print("score", score, type(score))
print()
# variable of type list (many values in one variable)
langs = ["Python", "JavaScript", "Java", "Bash"]
print("langs", langs, type(langs))
print("- langs[3]", langs[3], type(langs[3]))
print()
# variable of type dictionary (a group of keys and values)
person = {
"name": name,
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person))
print('- person["name"]', person["name"], type(person["name"]))
InfoDb = []
# Append to List a Dictionary of key/values
InfoDb.append({
"Food": "Chicken Alfredo",
"Cuisine": "Italian",
"Carbs": "Pasta",
"Fats": "Cream",
"Protein": "Chicken",
"Main_Ingredients": ["Pasta", "heavy cream", "butter", "chicken", "parmesan cheese"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"Food": "Chow Mein",
"Cuisine": "Chinese",
"Carbs": "Noodles",
"Fats": "Oil",
"Protein": "Meats or eggs",
"Main_Ingredients": ["Noodles", "oil", "vegetables", "different meats", "sauce(sometimes)"]
})
# Append to List a 3rd dictionary
InfoDb.append({
"Food": "Cheeseburger",
"Cuisine": "American",
"Carbs": "Hamburger Buns",
"Fats": "Meat and some sauces",
"Protein": "Meat",
"Main_Ingredients": ["Beef", "lettuce", "cheese", "tomato", "buns", "sauce"]
})
InfoDb.append({
"Food": "Poutine",
"Cuisine": "Canadian",
"Carbs": "None",
"Fats": "Fries and cheese curds",
"Protein": "Gravy",
"Main_Ingredients": ["Fries", "gravy", "cheese curds"]
})
InfoDb.append({
"Food": "Ramen",
"Cuisine": "Japanese",
"Carbs": "Noodles",
"Fats": "Oil",
"Protein": "Egg, meat",
"Main_Ingredients": ["Noodles", "broth", "egg", "chashu pork"]
})
InfoDb.append({
"Food": "Tacos",
"Cuisine": "Mexican",
"Carbs": "Tortilla",
"Fats": "Sauce",
"Protein": "Meat",
"Main_Ingredients": ["Tortilla", "different meats", "pico de gallo", "guacamole"]
})
# Print the data structure
print(InfoDb)
def print_data(d_rec):
print(d_rec["Food"]) # using comma puts space between values
print("\t", "Cuisine:", d_rec["Cuisine"]) # \t is a tab indent
print("\t", "Carbs:", d_rec["Carbs"])
print("\t", "Fats:", d_rec["Fats"])
print("\t", "Protein:", d_rec["Protein"])
print("\t", "Main Ingredients: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Main_Ingredients"])) # join allows printing a string list with separator
print()
# for loop iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_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()
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)
List1 = []
global List1
List1.append({
"Food": "Chicken Alfredo",
"Cuisine": "Italian",
"Carbs": "Pasta",
"Fats": "Cream",
"Protein": "Chicken",
"Main_Ingredients": ["Pasta", "heavy cream", "butter", "chicken", "parmesan cheese"]
})
#Create List2
List2 = []
global List2
List2.append({
"Food": "Chow Mein",
"Cuisine": "Chinese",
"Carbs": "Noodles",
"Fats": "Oil",
"Protein": "Meats or eggs",
"Main_Ingredients": ["Noodles", "oil", "vegetables", "different meats", "sauce(sometimes)"]
})
#Create List3
List3 = []
global List3
List3.append({
"Food": "Cheeseburger",
"Cuisine": "American",
"Carbs": "Hamburger Buns",
"Fats": "Meat and some sauces",
"Protein": "Meat",
"Main_Ingredients": ["Beef", "lettuce", "cheese", "tomato", "buns", "sauce"]
})
#Create List4
List4 = []
global List4
List4.append({
"Food": "Poutine",
"Cuisine": "Canadian",
"Carbs": "None",
"Fats": "Fries and cheese curds",
"Protein": "Gravy",
"Main_Ingredients": ["Fries", "gravy", "cheese curds"]
})
#Create List5
List5 = []
global List5
List5.append({
"Food": "Ramen",
"Cuisine": "Japanese",
"Carbs": "Noodles",
"Fats": "Oil",
"Protein": "Egg, meat",
"Main_Ingredients": ["Noodles", "broth", "egg", "chashu pork"]
})
#Create List6
List6 = []
global List6
List6.append({
"Food": "Tacos",
"Cuisine": "Mexican",
"Carbs": "Tortilla",
"Fats": "Sauce",
"Protein": "Meat",
"Main_Ingredients": ["Tortilla", "different meats", "pico de gallo", "guacamole"]
})
# Print the data structure to test if it works
print(List1)
print(List2)
import getpass, sys
def print_data(d_rec):
print(d_rec["Food"]) # using comma puts space between values
print("\t", "Cuisine:", d_rec["Cuisine"]) # \t is a tab indent
print("\t", "Carbs:", d_rec["Carbs"])
print("\t", "Fats:", d_rec["Fats"])
print("\t", "Protein:", d_rec["Protein"])
print("\t", "Main_Ingredients: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Main_Ingredients"])) # join allows printing a string list with separator
print()
# Create response prompt
def question_with_response(prompt):
print(prompt)
msg = input()
return msg
# Create the loops for the quiz(Note: there are some cursed squiggly lines showing an error. There is in fact no error, it is probably just a glitch when switching between coding languages.)
def for_loop1():
for record in List1:
print_data(record)
def for_loop2():
for record in List2:
print_data(record)
def for_loop3():
for record in List3:
print_data(record)
def for_loop4():
for record in List4:
print_data(record)
def for_loop5():
for record in List5:
print_data(record)
def for_loop6():
for record in List6:
print_data(record)
# Confirmation to take the quiz
print('Hello, ' + getpass.getuser() + " running on" + sys.executable + "!")
print("You will be asked to type a food you want to learn more on. You can search for the foods up to six times. If you want to continue searching for more foods, please refresh the cell.")
question_with_response("Are you ready to explore different foods?")
# Question 1
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
else:
print("Try putting in a different food!")
# Question 2
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
else:
print("Try putting in a different food!")
# Question 3
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
else:
print("Try putting in a different food!")
# Question 4
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
else:
print("Try putting in a different food!")
# Question 5
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
else:
print("Try putting in a different food!")
# Question 6 along with end of cell
rsp = question_with_response("What food would you like to explore?")
if rsp == "Chicken alfredo":
print("Here's some info about your food:")
for_loop1()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
elif rsp == "Chow mein":
print("Here's some info about your food:")
for_loop2()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
elif rsp == "Cheeseburger":
print("Here's some info about your food:")
for_loop3()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
elif rsp == "Poutine":
print("Here's some info about your food:")
for_loop4()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
elif rsp == "Ramen":
print("Here's some info about your food:")
for_loop5()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
elif rsp == "Tacos":
print("Here's some info about your food:")
for_loop6()
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
else:
print("You've used up all your searches. Please refresh the cell if you want to continue searching for different foods.")
food = ["pizza", "burgers", "fries", "pasta"]
global food
print("food", food, type(food), "length",len(food))
print(food[1], type(food[1]))
def for_loop():
print("Food for loop output:")
for record in food:
print(record)
for_loop()