1. Introduction
Creating a Quiz App in Python is a great way to learn and practice handling data structures, functions, loops, and conditional statements. This tutorial will guide you through building a console-based Quiz App that not only tests knowledge but also provides explanations for the answers and calculates the score.
2. Program Steps
1. Define a quiz structure with questions, options, correct answers, and explanations.
2. Display each question to the user, showing the options for answers.
3. Take the user's answer as input and validate it.
4. Show the correct answer's explanation regardless of the user's answer.
5. Calculate and display the user's score at the end of the quiz.
3. Code Program
# Quiz Data Structure containing 5 MCQs on Python
quiz = [
{
"question": "Which of the following data types is not supported in Python?",
"options": ["Lists", "Tuples", "Sets", "Arrays"],
"answer": "Arrays",
"explanation": "Arrays are not a built-in data type in Python. However, they can be implemented using modules like numpy."
},
{
"question": "What will be the output of the following code snippet: print(2 ** 3)?",
"options": ["6", "8", "9", "5"],
"answer": "8",
"explanation": "The ** operator in Python is used for exponentiation. 2 raised to the power of 3 equals 8."
},
{
"question": "What keyword is used to define a function in Python?",
"options": ["func", "define", "def", "function"],
"answer": "def",
"explanation": "In Python, the 'def' keyword is used to start the definition of a function."
},
{
"question": "Which of the following is used to comment a single line in Python?",
"options": ["//", "/* */", "#", "--"],
"answer": "#",
"explanation": "In Python, the hash (#) symbol is used to start writing a comment."
},
{
"question": "Which of the following loop structures does Python not support?",
"options": ["for", "while", "do-while", "All are supported"],
"answer": "do-while",
"explanation": "Python does not have a built-in support for the do-while loop."
}
]
score = 0
# Main program loop
for item in quiz:
print("\n" + item["question"])
for i, option in enumerate(item["options"], start=1):
print(f"{i}. {option}")
# Step 3: Taking user input
user_answer = input("Enter your answer (number): ")
# Validating and processing the input
if item["options"][int(user_answer) - 1] == item["answer"]:
print("Correct!")
score += 1
else:
print("Wrong!")
# Step 4: Showing explanation
print("Explanation:", item["explanation"])
# Step 5: Displaying the score
print(f"\nYour final score is: {score}/{len(quiz)}")
Output:
Which of the following data types is not supported in Python? 1. Lists 2. Tuples 3. Sets 4. Arrays Enter your answer (number): 4 Correct! Explanation: Arrays are not a built-in data type in Python. However, they can be implemented using modules like numpy. What will be the output of the following code snippet: print(2 ** 3)? 1. 6 2. 8 3. 9 4. 5 Enter your answer (number): 2 Correct! Explanation: The ** operator in Python is used for exponentiation. 2 raised to the power of 3 equals 8. What keyword is used to define a function in Python? 1. func 2. define 3. def 4. function Enter your answer (number): 3 Correct! Explanation: In Python, the 'def' keyword is used to start the definition of a function. Which of the following is used to comment a single line in Python? 1. // 2. /* */ 3. # 4. -- Enter your answer (number): 4 Wrong! Explanation: In Python, the hash (#) symbol is used to start writing a comment. Which of the following loop structures does Python not support? 1. for 2. while 3. do-while 4. All are supported Enter your answer (number): 3 Correct! Explanation: Python does not have a built-in support for the do-while loop. Your final score is: 4/5
Explanation:
1. The quiz is structured as a list of dictionaries, where each dictionary represents a quiz question, its options, the correct answer, and an explanation. This structure makes it easy to extend the quiz by adding more questions.
2. The program iterates through each question in the quiz, printing the question and its options and prompting the user for their answer. User input is expected to be the number corresponding to the option they believe is correct.
3. After the user inputs their answer, the program checks if it matches the correct answer. The user's score is incremented for correct answers. Regardless of whether the answer was correct, the correct answer's explanation is displayed. This feedback loop helps make the quiz a testing and a learning tool.
4. Once all questions have been answered, the program calculates the final score by comparing the correct answers to the total number of questions.
5. This tutorial demonstrates the basics of creating an interactive Python application, emphasizing data management, user input handling, and providing feedback. It's a foundation upon which more complex and feature-rich applications can be built.
Comments
Post a Comment
Leave Comment