Lists and Dictionary Hacks
List, Iterations, and Dictionaries with for loop
- Reversed Numbers
- New Record and Input record
- For Loop through index
- Quiz that Stores in list/dictionaries
- Hacks
num = ["one", "two", "three", "four", "five", "six"]
numC = 0k
for x in reversed(num):
if numC-2<len(num):
print("- num[",numC,"]", x, type(num[numC]))
numC += 1
import getpass, sys
InfoDb = []
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"]
})
InfoDb.append({
"FirstName": "Sarah",
"LastName": "Liu",
"DOB": "January 12",
"Residence": "San Diego",
"Email": "mail2sarahl@gmail.com",
"Owns_Cars": ["2010 Lexus IS 250"]
})
def collect_data():
print("Please enter your first name:")
fn = input()
print(fn)
print("Please enter your last name:")
ln = input()
print(ln)
print("Please enter your date of birth:")
dob = input()
print(dob)
print("Please enter your county:")
county = input()
print(county)
print("Please enter your favorite color:")
color = input()
print(color)
print("Please input your favorite movie:")
movie = input()
print(movie)
print("Please input your owned cars:")
Owns_Cars = input()
print(Owns_Cars)
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": fn,
"LastName": ln,
"DOB": dob,
"Residence": county,
"Fav_Color": color,
"Fav_movie": movie,
"Owns_Cars": Owns_Cars
})
def more_info():
while True:
ans = input("Do you want to add more info to the database? ")
if ans == "yes":
name = True
collect_data() #collect data if input is "yes"
print()
else: #stop function if input isn't "yes"
break
# Print the data structure
print(InfoDb)
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: ",d_rec["Owns_Cars"]) # end="" make sure no return occurs
# print(", ".join(d_rec["Owns_Cars"])) # 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()
questions = 6
correct = 0
def question_and_answer(prompt, answer):
print("Question: " + prompt)
inp = input()
print("Answer: " + inp)
if answer == inp.lower():
print("Good Job! That is the right answer!")
global correct
correct += 1
else:
print ("That is Wrong!")
return inp
Question1 = question_and_answer("Which country gifted the Statue of Liberty to the US? \t 1 for France \t 2 for England \t 3 for Germany \t 4 or Italy", "1")
Question2 = question_and_answer("What is the name of the longest river in South America? \t 1 for Mississippi River \t 2 for Amazon River", "2")
Question3 = question_and_answer("Which fictional city is the home of Batman? \t 1 for San Diego \t 2 for Central City \t 3 for Gotham City ", "3")
Question4 = question_and_answer("How many feet are in a mile?", "5280")
Question5 = question_and_answer(" Who is the author of the Harry Potter series \t 1 for JK Rowling \t 2 for Rick Riordan \t 3 for William Shakespeare", "1")
Q6 = question_and_answer("What number is the iron in the periodic table of elements?", "26")
print(correct, "answers correct", correct*100/questions,"%")
Quiz = {
"Question One": Question1 ,
"Question Two": Question2 ,
"Question Three ": Question3,
"Question Four ": Question4,
"Question Five": Question5,
"Question Six": Q6
}
print("Your answers:",Quiz)
Hacks
- Add a couple of records to the InfoDb
- Try to do a for loop with an index
- Pair Share code somethings creative or unique, with loops and data. Hints...
- Would it be possible to output data in a reverse order?
- Are there other methods that can be performed on lists?
- Could you create new or add to dictionary data set? Could you do it with input?
- Make a quiz that stores in a List of Dictionaries.