Python Lists and Quiz
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
#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": "Aniket",
"LastName": "Chakradeo",
"DOB": "Febuary 20",
"Age": "15",
"HairColor": "Black",
"Residence": "4S Ranch",
"Email": "aniket@chakradeo.net",
"Owns_Cars": ["Red-Buggati"]
})
InfoDb.append({
"FirstName": "Lucas",
"LastName": "Moore",
"DOB": "December 16",
"Age": "15",
"HairColor": "Blonde",
"Residence": "4S Ranch",
"Email": "lem16silver@gmail.com",
"Owns_Cars": ["None"]
})
InfoDb.append({
"FirstName": "Ryan",
"LastName": "Hakimipour",
"DOB": "June 15",
"Age": "15",
"HairColor": "Black",
"Residence": "San Diego",
"Email": "RyanHaki@gmail.com",
"Owns_Cars": ["None"]
})
InfoDb.append({
"FirstName": "Soham",
"LastName": "Kamat",
"DOB": "March 9",
"Age": "15",
"HairColor": "Black",
"Residence": "San Diego",
"Email": "sohamk10039@stu.powayusd.com",
"Owns_Cars": ["None"]
})
#print(InfoDb)
# 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: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print()
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
print("\nRecursive loop output:\n")
recursive_loop(0)
Function returning an input in reverse order
def reverse_function(string):
return string[::-1]
# prints string normally
string = input("Enter a word or phrase:")
# prints same string backwards, using the ::-1
mytxt = reverse_function(string)
print(string)
print(mytxt)
Printing a dictionary in reverse order using a for loop
aniket_dict = {'t': 1, 'e': 2, 'k': 3, 'i': 4, 'n': 5, 'A': 6}
for k in reversed(list(aniket_dict.keys())):
print(k)
def Python_Quiz(prompt):
global word
print ("Question: " + prompt)
word = input()
return word
My_Quiz = []
questions_number = 5
correct_answer = 0
print("Hello, you will be asked " + str(questions_number) + " short questions about common conversions.")
My_Quiz.append({
"How many inches are in a foot?": "12",
"How many kilometers is 1,000,000 millimeters?": "1",
"How many feet are in a yard?": "3",
"How many grams are in 25 kilograms?": "25000",
"What is KHDUDCM used for?": "Metric conversions",
})
# for loop checks answers with the value in the dictionary
for dict in My_Quiz:
for questions, answers in dict.items():
Python_Quiz(questions)
if word == answers:
print(input() + " is correct")
correct_answer += 1
else:
print(input() +" is incorrect")
print("You scored " + str(correct_answer) + "/" + str(questions_number) + ". Good work!")