- CodeCraft by Dr. Christine Lee
- Posts
- Blast Off with Python
Blast Off with Python
Create Your Own Solar System Quiz Today
Create a Fun Solar System Quiz with Python Dictionaries!
Hey there, future coding star! Today, we’re going to build an exciting quiz about the solar system using Python dictionaries. This project is perfect for beginners and will teach you how to use dictionaries, handle user input, and keep track of scores. Let’s jump into this fun coding adventure!
What You Will Learn
Dictionaries: How to store questions and answers.
Loops: How to ask questions repeatedly.
Conditionals: How to check answers.
User Input: How to get and use input from the player.
Score Calculation: How to calculate and display the score.
Step-by-Step Guide to Creating the Solar System Quiz
Step 1: Set Up the Quiz
First, we need to set up our quiz with a dictionary where the keys are questions and the values are the correct answers.
# Creating a dictionary with questions and answers
quiz = {
'What is the largest planet in our solar system?': 'Jupiter',
'Which planet is known as the Red Planet?': 'Mars',
'What planet is closest to the Sun?': 'Mercury',
'Which planet has the most moons?': 'Saturn',
'What is the smallest planet in our solar system?': 'Mercury'
'Which is the coldest planet in our solar system?': 'Uranus'
'Which is the hottest planet in our solar system?': 'Venus'
'Which is the 3rd planet from the sun?': 'Earth'
'Which is the 4th planet from the sun?': 'Mars'
'Which is the 5th planet from the sun?': 'Jupiter'
}
Explanation:
We’ve created a dictionary called quiz
that holds our questions as keys and the correct answers as values.
Step 2: Initialize the Score
We’ll set up a variable to keep track of the player’s score.
# Initialize score
score = 0
Explanation:
We’ve initialized the score
to zero. This will keep track of how many questions the player answers correctly.
Step 3: Ask the Questions
We’ll loop through each question in the dictionary, get the user’s answer, and check if it’s correct.
# Loop through the dictionary
for question, correct_answer in quiz.items():
print(question)
answer = input("Your answer: ").title().strip()
if answer == correct_answer:
print("Correct!")
score += 1
else:
print(f"Wrong! The correct answer is {correct_answer}.")
print() # Add a blank line for better readability
Explanation:
Loop: Using a
for
loop, we iterate through each question in the dictionary.User Input: We get the player’s answer and format it with
.title().strip()
to ensure it matches the format of our correct answers.Check Answer: Using the conditional statements (
if…else
), we compare the player’s answer to the correct answer and update the score if they match.
Step 4: Calculate and Display the Score
After the quiz, we’ll calculate the score as a percentage and display it.
# Calculate the score percentage
total_questions = len(quiz)
score_percentage = (score / total_questions) * 100
print(f"Your final score is {score} out of {total_questions} questions.")
print(f"That's {score_percentage:.2f}%. Well done!")
Explanation:
Total Questions: We get the total number of questions using the
len()
.Score Percentage: We calculate the score percentage.
Display Score: We print the final score and the percentage.
Recommendation: Be inspired by Cool Stories about People Making Money
|
Conclusion
And there you have it! You’ve just created a fun and interactive Solar System Quiz in Python. This project introduced you to using dictionaries, loops, conditionals, and score calculation—all essential skills for any budding programmer.
Ready for More Python Adventures?
Subscribe to our newsletter now and get a free Python cheat sheet! Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.
Keep exploring, keep learning, and have a blast with Python!