Python Quiz and Hacks
My own quiz with my own questions and Mr. Mortensen's questions.
My Quiz
This quiz defines two functions for asking questions and receiving an answer. The quiz has 7 questions. Each question asked is compared to a correct answer and if they are equal (with no regard to case) then the quiz tells the user. If not, then the quiz tells the user that the answer is wrong. The program stores each correct answer and tells the user the percentage of correct answers at the end of the quiz.
import getpass, sys # imports functions and libraries
def question_and_answer(prompt): # defines the question_and_answer function
print("Question: " + prompt) # asks the user a question
msg = input() # takes the user's input
print("Answer: " + msg) # prints the answer and the user's input
def question_with_response(prompt): # defines the question_with_response function
print("Question: " + prompt) # asks the user a question
msg = input() # takes the user's input
return msg # the function returns the user's response as a string value
questions = 7 # number of questions in the quiz
correct = 0 # amount correct in the beginning of the quiz
print('Hello, ' + getpass.getuser() + " running " + sys.executable) # greets the user
print("You will be asked " + str(questions) + " questions.") # tells the user how many questions are in the quiz
question_and_answer("Are you ready to take a test?") # prompts the user to start the quiz
rsp = question_with_response("What command is used to include other functions that were previously developed?") # asks question and stores the user's response
if rsp.lower() == "import": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("What command is used to evaluate correct or incorrect response in this example?") # asks question and stores the user's response
if rsp.lower() == "if": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("Each 'if' command contains an '_________' to determine a true or false condition?") # asks question and stores the user's response
if rsp.lower() == "expression": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("What does 'def' do in Python in relation to a function: It _______ the function?") # asks question and stores the user's response
if rsp.lower() == "defines": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("What does the function take as a parameter?") # asks question and stores the user's response
if rsp.lower() == "prompt": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("Can static text change?") # asks question and stores the user's response
if rsp.lower() == "no": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
rsp = question_with_response("What is the first thing that beginner programmers usually print?") # asks question and stores the user's response
if rsp.lower() == "hello world": # compares the user's response to the answer without regard to the case
print(rsp + " is correct!") # shows that it is correct
correct += 1 # one more correct answer
else: # if not correct
print(rsp + " is incorrect!") # shows that it is incorrect
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions)+" or "+str((correct/questions)*100)+"%") # tells the user how many questions they got correct out of the total questions and their score