Learn Python in a gamified way, making programming as fun as magic!
Why is Python the “magic wand” of programming?
Python is like Harry Potter’s magic wand—easy to learn yet powerful! It is used by NASA for space exploration, by Netflix for movie recommendations, and even to control smart homes! Most importantly, it is very suitable for programming beginners:
graph LR
A[Beginner] --> B[Python Basics]
B --> C[Mini Games]
C --> D[Web Development]
D --> E[Artificial Intelligence]
Magic Lesson One: The Basics of Python “Spells”
Variables – Your Magic Storage Bag
# Create a magic storage bag
magic_bag = "wand" # String storage bag
spell_count = 3 # Numeric storage bag
is_magician = True # Boolean storage bag
print(f"My storage bag contains: {magic_bag}")
print(f"Remaining spell count: {spell_count}")
Conditional Statements – The Magic Decision Tree
weather = "sunny"
if weather == "sunny":
print("Use the sunshine spell!☀️")
elif weather == "rainy":
print("Use the waterproof spell!☔")
else:
print("Use the universal protection spell!🛡️")
Loops – The Magic Repetition Technique
# Cast the spell 3 times
for i in range(3):
print(f"Casting spell for the {i+1} time: Abracadabra!✨")
# Prepare magic materials
ingredients = ["dragon scales", "phoenix feathers", "unicorn tears"]
for item in ingredients:
print(f"Add: {item}")
Magic Practice: Create Your First Python Game
Magic Number Guessing Game
import random
print("""
=====================
Magic Number Guessing Game
=====================
Rules:
1. I will think of a magic number between 1-100
2. You have 7 chances to guess it
3. Each time I will hint "too high" or "too low"
""")
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 7
while attempts < max_attempts:
attempts += 1
print(f"\n=== Attempt {attempts} ===")
try:
guess = int(input("Please enter your guess:"))
except:
print("⚠️ Please enter a number!")
continue
if guess == secret_number:
print(f"\n✨✨✨ Awesome! ✨✨✨\nYou guessed the magic number {secret_number} on attempt {attempts}!\nYou have become a qualified magic apprentice!\n")
break
elif guess < secret_number:
print("🔮 The magic crystal glows blue: too low!")
else:
print("🔮 The magic book pages flip quickly: too high!")
if attempts == max_attempts:
print(f"\n💔 Unfortunately, you ran out of chances!\nThe magic number was {secret_number}\nCome back to the magic academy to learn again!\n")
Game Code Analysis: The Science Behind the Magic
1. Random Number Generation – The Magic Crystal Ball
secret_number = random.randint(1, 100) # Generate a random number between 1-100
2. Loop Control – The Magic Hourglass
while attempts < max_attempts: # Continue while attempts are less than 7
3. Conditional Branching – The Magic Decision Tree
if guess == secret_number: # Guessed correctly
elif guess < secret_number: # Guessed too low
else: # Guessed too high
4. Exception Handling – The Magic Shield
try:
guess = int(input("Please enter your guess:"))
except:
print("⚠️ Please enter a number!") # Prevent non-numeric input
Magic Upgrade: Adding Game Features
Add Difficulty Selection
print("Select difficulty:")
print("1. Apprentice (1-50, 10 chances)")
print("2. Mage (1-100, 7 chances)")
print("3. Archmage (1-200, 5 chances)")
level = input("Please enter difficulty number:")
if level == "1":
max_num = 50
max_attempts = 10
elif level == "2":
max_num = 100
max_attempts = 7
else:
max_num = 200
max_attempts = 5
secret_number = random.randint(1, max_num)
Add Scoring System
score = 100 # Initial score
# Deduct score after each guess
score -= 10
# Display score at the end of the game
print(f"Your final score: {score} points")
Magic Laboratory: Run Your Game
-
Install Python:
- Visit python.org
- Download the latest version (check “Add Python to PATH” during installation)
Create Game File:
- Create a text file named
<span>magic_game.py</span> - Copy the game code above into the file
Run the Game:
- Open Command Prompt (Windows) or Terminal (Mac/Linux)
- Type:
<span>python magic_game.py</span> - Start your magical journey!
Magic Secrets: Recommended Learning Resources
Best Resources for Beginners
| Resource | Features | Link |
|---|---|---|
| Official Python Tutorial | Officially produced, authoritative and reliable | docs.python.org |
| Codecademy | Interactive learning, instant feedback | codecademy.com |
| Rookie Tutorial | Chinese tutorial, easy to understand | runoob.com |
Next Steps in Magic Training
- Modify game prompts (make magic more interesting)
- Add a “Play Again” feature
- Record high scores
- Add a graphical interface (using Pygame)
Conclusion: Your Magical Journey Begins!
“Programming is not about understanding computers, but about understanding the world.” – Alan Kay
Through this simple number guessing game, you have learned:
- Variables store data
- Conditional statements make decisions
- Loops repeat actions
- Handle user input
- Use random numbers
pie
title Python Learning Path
"Basic Syntax" : 30
"Mini Project Practice" : 25
"Common Library Learning" : 20
"In-depth Professional Direction" : 15
"Participate in Open Source Projects" : 10
The door to the world of Python has been opened for you! Remember:
- Daily practice is more important than learning a lot at once
- Making mistakes is normal, error messages are the best teachers
- Start by imitating, gradually create your own projects
- Join the community, and communicate with other “wizards”