Python Quiz
This quiz will test your knowledge of the key vocabulary from the Jupyter document this quiz came from. Below you’ll see a version of the original code I modified in order to make more concise and simple.
import getpass, sys
# new function returns a boolean value that determines whether or not the answer is right. Eliminates the need for if else blocks to check answers
def question_with_response(prompt, answer):
print("Question:", prompt)
msg = input()
return msg == answer
# I put the questions and asnwers in a list and their corresponding answers as tuples. This gets rid of the use of rsp which was very repetitive in the original code
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")
]
correct = 0
# I used F strings to format this message to make the strings below much more concise and so that there's no need to insert the pluses
print(f'Hello, {getpass.getuser()} running {sys.executable}')
print(f'You will be asked {len(questions)} questions.')
question_with_response("Are you ready to take a test?", "yes")
# Here, instead of the repetitive if else block, I put everything in a loop that goes through each question in the list.
for prompt, answer in questions:
if question_with_response(prompt, answer):
print(f'{answer} is correct!')
correct += 1
else:
print(f'{answer} is incorrect!')
# You can calculate the score using len(questions) and the function labled as the variable percentage
total_questions = len(questions)
percentage = (correct / total_questions) * 100
print(f'{getpass.getuser()}, you scored {correct}/{total_questions} ({percentage:.2f}%)')
Hello, dslee187 running /bin/python
You will be asked 3 questions.
Question: Are you ready to take a test?
Question: What command is used to include other functions that were previously developed?
import is correct!
Question: What command is used to evaluate correct or incorrect response in this example?
if is correct!
Question: Each 'if' command contains an '_________' to determine a true or false condition?
expression is correct!
dslee187, you scored 3/3 (100.00%)