My Loop Marvel 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": "Derek",
"LastName": "Sol",
"DOB": "January 17",
"Residence": "San Diego",
"Email": "dmsol218@gmail.com",
"Owns_Cars": ["2018 Toyota Camry SE"]
})
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()
# for loop algorithm iterates on length of InfoDb
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)
q1 = """What is Captain America's shield made out of?
a. Vibranium
b. Steel
c. Iron
d. Copper"""
q2 = """How many Infinity Stone are there?
a. 4
b. 6
c. 3
d. 8"""
q3 = """Who lifts Thor's hammer in Endgame?
a. Iron Man
b. Vision
c. Captain America
d. Captain Marvel"""
questions = {q1 : "a",q2 : "b",q3 : "c"}
name = input("Enter your name: ")
print("Hello",name, "welcome to the this Marvel quiz, lets get started...")
score=0
for i in questions:
print(i)
ans = input("enter the answer(a/b/c/d) : ")
if ans==questions[i]:
print("correct answer, you got 1 point")
score = score+1
else:
print("wrong answer, you lost 1 point")
score=score-1
print("Final score is:", score)
def new_game():
guesses = []
correct_guesses = 0
question_num = 1
for key in questions:
print("-------------------------")
print(key)
for i in options[question_num-1]:
print(i)
guess = input("Enter (A, B, C, or D): ")
guess = guess.upper()
guesses.append(guess)
correct_guesses += check_answer(questions.get(key),guess)
question_num += 1
display_score(correct_guesses, guesses)
# -------------------------
def check_answer(answer, guess):
if answer == guess:
print("CORRECT!")
return 1
else:
print("WRONG!")
return 0
# -------------------------
def display_score(correct_guesses, guesses):
print("-------------------------")
print("RESULTS")
print("-------------------------")
print("Answers: ", end="")
for i in questions:
print(questions.get(i), end =" ")
print()
print("Guesses: ", end="")
for i in guesses:
print(i, end =" ")
print()
score = int((correct_guesses/len(questions))*100)
print("Your score is: "+str(score)+"%")
# -------------------------
def play_again():
response = input("Do you want to play again?: (yes or no): ")
response = response.upper()
if response == "YES":
return True
else:
return False
# -------------------------
questions = {
"What is Captain America's shield made out of?: ": "A",
"How many Infinity Stones are there?: ": "B",
"Who lifts Thor's hammer in Endgame?: ": "C",
"Who holds the Time Stone?: ": "A",
}
options = [["A. Vibranium", "B. Copper", "C. Iron", "D. Steel"],
["A. 4", "B. 6", "C. 3", "D. 2"],
["A. Iron Man", "B. Vision", "C. Captain America", "D. Captain Marvel"],
["A. Doctor Strange", "B. Ant-Man", "C. Vision", "D. Thor"]]
new_game()
while play_again():
new_game()
print("Bye!")