import getpass
import unittest.mock
# Define the quiz dictionary with questions as keys and answers as values
quiz = {
"Communication, consensus building, conflict resolution and negotiation is an example of ": "collaboration",
"Should the acknowledgment of a code segment include the origin or original author’s name?": "yes",
"The variable age is to be used to represent a person’s age, years. Which of the following is the most appropriate data type for age ": "integer",
"The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is the most appropriate data type for isOpen ?": "boolean",
"True or False? Program development is usually a solo endeavor." : "false",
"A group of statements is called.." : "code segment."
}
# Function to administer the quiz
def run_quiz():
total_questions = len(quiz)
user_responses = []
for i, (question, correct_answer) in enumerate(quiz.items(), start=1):
print(f"\nQuestion {i}: {question}")
user_answer = input("Your answer: ").strip().lower()
user_responses.append(user_answer)
# Calculate the score as a percentage
score = sum(user_answer == correct_answer for user_answer, correct_answer in zip(user_responses, quiz.values()))
percentage_score = (score / total_questions) * 100
print(user_responses)
print("\nUser Responses:")
for i, response in enumerate(user_responses, start=1):
print(f"Question {i}: {response}")
print(f"\nQuiz completed!\n{getpass.getuser()}, you scored {percentage_score:.2f}%.")
# Display user inputs as a list
print("\nUser Inputs:")
for i, response in enumerate(user_responses, start=1):
print(f"{i}. {response}")
# Calculate the percentage of questions answered correctly and incorrectly
correct_answers = 0
incorrect_answers = 0
for user_answer, correct_answer in zip(user_responses, quiz.values()):
if user_answer == correct_answer:
correct_answers += 1
else:
incorrect_answers += 1
percentage_correct = (correct_answers / total_questions) * 100
percentage_incorrect = (incorrect_answers / total_questions) * 100
print(f"\nPercentage correct: {percentage_correct:.2f}%")
print(f"Percentage incorrect: {percentage_incorrect:.2f}%")
# Tester function to validate quiz functionality
def test_quiz():
# Add test cases to validate quiz functionality
test_cases = [
(["collaboration", "yes", "integer", "boolean", "false", "code segment."], 100.0, 100.0, 0.0),
(["collaboration", "no", "integer", "boolean", "true", "code"], 33.33, 33.33, 66.67),
]
for i, (test_input, expected_percentage_score, expected_percentage_correct, expected_percentage_incorrect) in enumerate(test_cases, start=1):
print(f"\nTest Case {i} - Expected Score: {expected_percentage_score}%")
# Create a mock input object
input_mock = unittest.mock.Mock()
# Set the side effect of the mock input object to return the test input
input_mock.side_effect = test_input
# Patch the builtins.input function with the mock input object
with unittest.mock.patch("builtins.input", input_mock):
# Run the quiz function
run_quiz()
print("\n" + "-" * 40)
if __name__ == "__main__":
test_quiz() # Run the tester function
Test Case 1 - Expected Score: 100.0%
Question 1: Communication, consensus building, conflict resolution and negotiation is an example of
Question 2: Should the acknowledgment of a code segment include the origin or original author’s name?
Question 3: The variable age is to be used to represent a person’s age, years. Which of the following is the most appropriate data type for age
Question 4: The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is the most appropriate data type for isOpen ?
Question 5: True or False? Program development is usually a solo endeavor.
Question 6: A group of statements is called..
['collaboration', 'yes', 'integer', 'boolean', 'false', 'code segment.']
User Responses:
Question 1: collaboration
Question 2: yes
Question 3: integer
Question 4: boolean
Question 5: false
Question 6: code segment.
Quiz completed!
cliang, you scored 100.00%.
User Inputs:
1. collaboration
2. yes
3. integer
4. boolean
5. false
6. code segment.
Percentage correct: 100.00%
Percentage incorrect: 0.00%
----------------------------------------
Test Case 2 - Expected Score: 33.33%
Question 1: Communication, consensus building, conflict resolution and negotiation is an example of
Question 2: Should the acknowledgment of a code segment include the origin or original author’s name?
Question 3: The variable age is to be used to represent a person’s age, years. Which of the following is the most appropriate data type for age
Question 4: The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is the most appropriate data type for isOpen ?
Question 5: True or False? Program development is usually a solo endeavor.
Question 6: A group of statements is called..
['collaboration', 'no', 'integer', 'boolean', 'true', 'code']
User Responses:
Question 1: collaboration
Question 2: no
Question 3: integer
Question 4: boolean
Question 5: true
Question 6: code
Quiz completed!
cliang, you scored 50.00%.
User Inputs:
1. collaboration
2. no
3. integer
4. boolean
5. true
6. code
Percentage correct: 50.00%
Percentage incorrect: 50.00%
----------------------------------------
# for example...
def test_even_odd(numbers_list):
# Count the number of elements in the list
num_elements = len(numbers_list)
# Check if it's even or odd using the modulo operator
if num_elements % 2 == 0:
print("The list has an even number of elements.")
else:
print("The list has an odd number of elements.")
# Example usage
even_list = [1, 2, 3, 4, 5, 6]
odd_list = [1, 2, 3, 4, 5]
print("Even list:")
test_even_odd(even_list)
print("\nOdd list:")
test_even_odd(odd_list)