Lists, Dictionaries, Iteration
Data Abstraction using Python Lists [] and Python Dictionaries {}.
name = "Mani T"
print("name", name, type(name))
age = 17
print("age", age, type(age))
score = 100.0
print("score", score, type(score))
print()
langs = ["Python", "Bash", "HTML"]
print("langs", langs, type(langs))
print("- langs[0]", langs[0], type(langs[0]))
print()
person = {
"name": name, # keys and values
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person))
print('- person["name"]', person["name"], type(person["name"]))
InfoDb = []
InfoDb.append({ #introduces the data
"FirstName": "Mani",
"LastName": "Taleban",
"DOB": "May 8",
"Residence": "Somewhere",
"SE": "moeint56403@stu.powayusd.com",
"PE": "tmanimasih@gmail.com",
"School": "Del Norte High School",
"FM": "Whiplash",
"FB": "Radiohead"
})
print(InfoDb)
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # comma = space
print("\t", "Residence:", d_rec["Residence"]) # \t = tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "School Email:", d_rec["SE"])
print("\t", "Personal Email:", d_rec["PE"])
print("\t", "School:", d_rec["School"])
print("\t", "Favorite Movie:", d_rec["FM"])
print("\t", "Favorite Band:", d_rec["FB"])
print()
def for_loop(): # for loop
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
InfoDb = []
InfoDb.append({
"FirstName": "Mani",
"LastName": "Taleban",
"DOB": "May 8",
"Residence": "Somewhere",
"SE": "moeint56403@stu.powayusd.com",
"PE": "tmanimasih@gmail.com",
"School": "Del Norte High School",
"FM": "Whiplash",
"FB": "Radiohead"
})
print(InfoDb)
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()
import getpass ,sys
def question_and_answer(prompt, answer): # question and answer function
print("Question: " + prompt)
msg = input()
print("Answer: " + msg)
if answer == msg.lower(): # this conditional determines the answers
print(":)")
global correct
correct += 1
else:
print (":(")
questions = 5
correct = 0
Q1 = question_and_answer("What should I listen to right now?", "music")
Q2 = question_and_answer("When was I born?", "may 8")
Q3 = question_and_answer("Why was I born?", "idk")
Q4 = question_and_answer("Who plays Fletcher in Whiplash?", "jk simmons")
Q5 = question_and_answer("How's it going?", "eh")
Questions = { # infoDb
"Q1": Q1,
"Q2": Q2,
"Q3": Q3,
"Q4": Q4,
"Q5": Q5
}
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))