Lists, Dictionaries, Iteration
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "Sarah Liu"
print("name", name, type(name))
print()
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
age = 16
print("age", age, type(age))
print()
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
score = 90.0
print("score", score, type(score))
print()
print("What is variable name/key?", "value?", "type?", "primitive or collection?")
print("What is different about the list output?")
langs = ["Python", "JavaScript", "Java", "Bash"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[0]", langs[0], type(langs[0]))
print()
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
print("What is different about the dictionary output?")
person = {
"name": name,
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person), "length", len(person))
print('- person["name"]', person["name"], type(person["name"]))
InfoDb = []
InfoDb.append({
"FirstName": "Sarah",
"LastName": "Liu",
"DOB": "January 12",
"Residence": "San Diego",
"Favorite Artist": "Kanye West",
"Favorite Color": "Red",
})
InfoDb.append({
"FirstName": "Amay",
"LastName": "Advani",
"DOB": "June 10",
"Residence": "San Diego",
"Favorite Artist": "Kanye West",
"Favorite Color": "Blue",
})
print(InfoDb)
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "Residence:", d_rec["Residence"])
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Favorite Artist:", d_rec["Favorite Artist"])
print("\t", "Favortie Color:", d_rec["Favorite Color"])
print()
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)
import getpass, sys
def question_and_answer(prompt,answer):
print("Question: " + prompt)
msg = input()
if answer == msg.lower():
print("Correct Answer")
global correct
correct += 1
else:
print("Incorrect answer")
questions = 6
correct = 0
Question1 = question_and_answer("Area 51 is located in which US state? 1 for Nevada, 2 for California, 3 for Texas, 4 for Florida" , "1")
Question2 = question_and_answer("What is the nickname of the US state of California? 1 for Tree State, 2 for Desert State, 3 for Golden State, 4 for Orange State", "3")
Question3 = question_and_answer("Who is depicted on the US hundred-dollar bill? 1 for George Washington, 2 for Benjamin Franklin, 3 for Andrew Jackson, 4 for Abraham Lincoln", "2")
Question4 = question_and_answer("Who is the author of Jurrasic Park? 1 for Michael Crichton, 2 for JK Rowling, 3 for Stephen King, 4 for William Shakespeare", "1")
Question5 = question_and_answer("What is the last letter of the Greek alphabet? 1 for Alpha, 2 for Beta, 3 for Delta, 4 for Omega", "4")
Question6 = question_and_answer("Who is the mascot for the fast-food chain, KFC? 1 for Colonel Sanders, 2 for McDonalds Clown, 3 for Jack Box, 4 for Wendy", "1")
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": Question6,
}
print("Your answers:",Quiz)
alphabet = ["a","b", "c", "d"]
alphabetC = 0
for x in reversed(alphabet):
if alphabetC-2<len(alphabet):
print("- alphabet[",alphabetC,"]", x, type(alphabet[alphabetC]))
alphabetC += 1