0 XP
Chapter 3
Functions
Package code into reusable blocks — just like game actions.
Core Concept
Functions are named blocks of code you can call whenever you need them.
Theory
Define a function with
def, give it a name, and optionally some inputs (parameters):
def greet_player(name):
print(f"Welcome, {name}!")
greet_player("Alex") # calls the function
Functions can also return a value:
def calculate_damage(attack, defense):
return max(0, attack - defense)
dmg = calculate_damage(30, 10)
print(f"Damage dealt: {dmg}")
Live Demo — Run & Explore
Try it live
Loading...
Output will appear here…
Exercise 1 / 1
📝 Write a function called `is_game_over` that takes `lives` as a parameter and returns True if lives is 0, else False.
Your solution
Loading...
Output will appear here…
Ask AI Tutor
AI Tutorgemini-3.1-flash-lite-preview
AI
Hi! I'm your AI tutor for Functions. 🎓
Ask me anything about this lesson — what a variable is, why loops are useful, or "why is my code not working?". I'm here to help you understand, not just give you answers!
What's on your mind? 😊