# Define the quiz questions and answers
questions = [
{
"question": "What is Python?",
"options": ["A type of snake", "A high-level programming language", "A data structure", "A computer manufacturer"],
"correct_answer": "B"
},
{
"question": "Which of the following is not a valid Python data type?",
"options": ["Integer", "String", "Float", "Ladder"],
"correct_answer": "D"
},
{
"question": "How do you write a single-line comment in Python?",
"options": ["// This is a comment", "# This is a comment", "/* This is a comment */", "-- This is a comment"],
"correct_answer": "B"
},
{
"question": "Which of the following is used to define a function in Python?",
"options": ["def", "function", "define", "func"],
"correct_answer": "A"
},
{
"question": "What does the 'print()' function do in Python?",
"options": ["Reads user input", "Writes text to a file", "Displays output to the console", "Performs mathematical calculations"],
"correct_answer": "C"
},
{
"question": "How do you declare a variable in Python?",
"options": ["var x", "let x", "x =", "x :="],
"correct_answer": "C"
},
{
"question": "Which of the following is used to create a list in Python?",
"options": ["( )", "{ }", "[ ]", "< >"],
"correct_answer": "C"
},
{
"question": "What does the 'if' statement do in Python?",
"options": ["Loops through a sequence", "Defines a function", "Makes decisions based on conditions", "Prints text to the console"],
"correct_answer": "C"
},
{
"question": "Which operator is used for exponentiation in Python?",
"options": ["^", "**", "^", "%"],
"correct_answer": "B"
},
{
"question": "What is the output of '3 + 4 * 5' in Python?",
"options": ["35", "23", "27", "60"],
"correct_answer": "D"
}
]
# Initialize the score
score = 0
# Function to display and grade the quiz
def run_quiz(questions):
for i, question in enumerate(questions, 1):
print(f"Question {i}: {question['question']}")
for j, option in enumerate(question['options'], 1):
print(f"{chr(64+j)}. {option}")
user_answer = input("Your answer (A/B/C/D): ").upper()
if user_answer == question['correct_answer']:
print("Correct!\n")
global score
score += 1
else:
print(f"Wrong! The correct answer is {question['correct_answer']}.\n")
# Run the quiz
print("Welcome to the Python Quiz!")
run_quiz(questions)
# Display the final score
print(f"You scored {score} out of {len(questions)} questions.")
# Calculate and display the percentage score
percentage_score = (score / len(questions)) * 100
print(f"Percentage Score: {percentage_score:.2f}%")
# Provide feedback based on the score
if percentage_score >= 70:
print("Congratulations! You did well.")
else:
print("You might want to review your Python knowledge. Keep learning!")