Table of Contents
Introduction
Welcome to “Python for Kids: Coding a Planets Quiz” tutorial! In this tutorial, we will learn how to create a simple command-line quiz about planets using Python. By the end of this tutorial, you will be able to write your own quiz and customize it with different questions.
Prerequisites
To get the most out of this tutorial, you should have a basic understanding of Python syntax, data types, and control flow statements. It would also be helpful to have Python installed on your computer.
Setup
-
Make sure you have Python installed. You can check this by opening a terminal/console window and running the following command:
python --version
If you see Python version information, you’re good to go. Otherwise, visit the official Python website (https://www.python.org/) to download and install Python.
-
Choose a text editor or integrated development environment (IDE) for writing your Python code. Some popular options include Visual Studio Code, PyCharm, and Sublime Text.
-
Create a new directory on your computer where you will store the files for this project. For example, you could create a directory called “planets-quiz”.
mkdir planets-quiz
-
Change into the newly created directory.
cd planets-quiz
-
Open your preferred text editor or IDE and create a new Python file called “quiz.py”.
touch quiz.py
Now we are ready to dive into coding our planets quiz!
Creating the Quiz
-
Open the “quiz.py” file in your text editor or IDE.
-
At the beginning of the file, let’s define a list of dictionaries to store the quiz questions and their respective answers. Each dictionary will contain two key-value pairs: “question” and “answer”.
questions = [ {"question": "Which planet is closest to the Sun?", "answer": "Mercury"}, {"question": "Which planet is known as the Red Planet?", "answer": "Mars"}, {"question": "Which planet is the largest in our solar system?", "answer": "Jupiter"}, # Add more questions here ]
Feel free to add more questions to the list.
-
Next, let’s define a function called “run_quiz” that will run the quiz. Inside the function, we will iterate over the list of questions, present each question to the user, and check if their answer is correct.
def run_quiz(): score = 0 total_questions = len(questions) for question in questions: user_answer = input(question["question"] + " ") if user_answer.lower() == question["answer"].lower(): print("Correct!") score += 1 else: print("Incorrect!") print("Quiz completed!") print(f"You scored {score}/{total_questions}")
The function keeps track of the user’s score and provides feedback for each question.
-
To start the quiz, let’s call the “run_quiz” function at the end of the file.
run_quiz()
That’s it! We have created a simple planets quiz. Now let’s see how to run it.
Running the Quiz
-
Save the “quiz.py” file.
-
Open a terminal or console window and navigate to the “planets-quiz” directory.
cd planets-quiz
-
Run the quiz by executing the following command:
python quiz.py
-
The quiz will begin, and you will be presented with each question in the console. Enter your answer and press Enter.
Which planet is closest to the Sun? Mercury
-
After answering all the questions, the quiz will provide your score.
Quiz completed! You scored 2/3
Congratulations! You have successfully coded a planets quiz in Python.
Conclusion
In this tutorial, we learned how to create a simple planets quiz using Python. We started by defining a list of questions and their respective answers. Then we wrote a function to run the quiz, presenting questions to the user and checking their answers. Finally, we saw how to run the quiz and receive the score.
You can customize the quiz by adding more questions or modifying the existing ones. You might also consider adding a timer or a high-score feature to enhance the quiz further.
I hope you enjoyed this tutorial and that it motivated you to explore more Python programming possibilities. Keep coding and having fun!