Python Quiz
You can use the following quiz to test your Python knowledge!
def ask(qna):
# destructure qna tuple
question, answer = qna
# get user input
res = input(question + " ")
# make comparison case-insensitive (given our answers are lowercase)
if res.lower() == answer:
# use string interpolation to print a correct anser
print("'{}' is the correct answer!".format(res))
return True
else:
# prompt the user to try again if they want to
again = input("'{}' is incorrect. Try again? [y/N] ".format(res))
if again == "y":
# recursively call "ask" function for the retry feature
return ask(qna)
else:
return False
questions_and_answers = [
# Pre-written questions
("What command is used to include other functions that were previously developed?", "import"),
("What command is used to evaluate correct or incorrect response in this example?", "if"),
("Each 'if' command contains an '_________' to determine a true or false condition?", "expression"),
# My own questions
("The two possible boolean values in Python are true and '____'.", "false"),
("Python is a(n) [interpreted/compiled] language.", "interpreted"),
("A '___' loop can iterate over items in a list.", "for"),
("What keyword can be used to create a block that can catch and handle an exception?", "try"),
# Questions for the document
("A function '________' allows the programmer to specify additional info to a function.", "parameter"),
("The 'def' keyword is used when defining a '________'.", "function"),
("The '_' operator can be used to concatenate two strings.", "+")
]
### MAIN ###
print("Welcome to the Python quiz!")
# counter for the total correct answers
correct = 0
# loop over all items in the questions_and_answers list
for qna in questions_and_answers:
if ask(qna):
# increment the counter if the user gets something correct
correct += 1
# display the total score at the end with an encouraging message
print("Great job! You got a score of {}%.".format(
int(100 * correct / len(questions_and_answers))))
Here's a screenshot of my quiz running as a separate Python file in my terminal to "show workflow of Input and Output in terminal":