Lists, Dictionaries, and Iterations
InfoDb = []
InfoDb.append({
"First_Name": "Shruthi",
"Last_Name": "Malayath",
"DOB": "April 14 2006",
"Email": "shmalayath@gmail.com",
"Hobbies": "Watching movies, drawing, and sleeping",
"Favorite_Color": ["green"],
}
InfoDb.append({
"First_Name": "Kavya",
"Last_Name": "Malayath",
"DOB": "August 30 2009",
"Email": "kavyamalayath@com",
"Hobbies": "Reading",
"Favorite_Color": ["purple"],
})
InfoDb.append({
"First_Name": "Snow",
"Last_Name": "Malayath",
"DOB": "March 31 2020",
"Email": "she's a dog",
"Hobbies": "chewing furniture",
"Favorite_Color": ["blue"],
})
print(InfoDb)
def print_data(d_rec):
print(d_rec["First_Name"], d_rec["Last_Name"]) # using comma puts space between values
print("\t", "DOB:", d_rec["DOB"]) # \t is a tab indent
print("\t", "email:", d_rec["Email"])
print("\t", "Hobbies:", d_rec["Hobbies"])
print("\t", "Favorite Color: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Favorite_Color"])) # join allows printing a string list with separator
print()
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("Information on:\n")
for record in InfoDb:
print_data(record)
for_loop()
import getpass, sys
questions = 6
correct = 0
print('Hello, ' + getpass.getuser())
print("\t", "You will be asked " + str(questions) + " questions on world history.")
print("\t", getpass.getuser() + " you must use proper capitalization on this quiz!!")
def question_and_answer(prompt, answer):
print("Question: " + prompt)
rsp = input()
if rsp == answer :
print("\t", rsp + " is correct!")
global correct
correct += 1
else:
print ("\t", rsp + " is incorrect!")
return rsp
Question_1 = question_and_answer("What year did WWI start?", "1914")
Question_2 = question_and_answer("The event in 1968 where Soviet troops stormed Czechoslovakia to crack down on reforms:", "Prague Spring")
Question_3 = question_and_answer("What is the first name of the Soviet Union president involved in the Cuban Missile Crisis", "Nikita")
Question_4 = question_and_answer("When were India and Pakistan divided?", "1947")
Question_5 = question_and_answer("The _____ dynasty marks the last of the Chinese emperors", "Qing")
Question_6 = question_and_answer("Due to liberation of many African nations, the year 1960 is known as the...", "Year of Africa")
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions) + "!!")
Quiz = []
Quiz.append({
"Q_1": Question_1,
"Q_2": Question_2,
"Q_3": Question_3,
"Q_4": Question_4,
"Q_5": Question_5
})
def print_data(d_rec):
print("Question 1:", d_rec["Q_1"])
print("Question 2:", d_rec["Q_2"])
print("Question 3:", d_rec["Q_3"])
print("Question 4:", d_rec["Q_4"])
print("Question 5:", d_rec["Q_5"], end="")
print("\n \n \n")
print("-----------------------------------------")
print("Here is a record of your quiz:")
def for_loop():
print("For loop output\n")
for record in Quiz:
print_data(record)
for_loop()