🐸

The Style Guide

Python Basicspython-basics-33-the-style-guide
Reward: 100 XP
|

The Style Guide

As Hoppy's magic grows, his spellbook is getting messy. The Wizard shows him a way to write "clean code" that anyone can understand.

In modern Python (3.11+), we use Type Hints to tell exactly what kind of magic (data) goes where.

Explicit Types

# Old way (Implicit)
name = "Hoppy"
power = 10

# New way (Explicit Type Hints)
name: str = "Hoppy"
power: int = 10

Function Blueprints

We can also specify what goes IN and what comes OUT of a function.

def cast_fireball(mana: int) -> str:
  return "Fireball!"

This says: cast_fireball takes an integer (mana) and returns a string.

Your Mission

1
Variables

Create a variable hero_name with the value "Hoppy" and explicitly mark it as a str.

2
Function

Define a function heal(hp: int) -> int.

3
Logic

Inside heal, return hp + 10.

4
Call

Call heal(50) and print the result.

Suggested Solution
Click to expand

Type hints don't change how code runs, but they help you catch bugs early!

hero_name: str = "Hoppy"

def heal(hp: int) -> int:
  return hp + 10

print(heal(50))
Advanced Tips
Want more? Click to expand

Tools like mypy or IDEs (VS Code) use these hints to warn you if you try to put a str where an int belongs.

pymain.py
Loading...
Terminal
Terminal
Ready to run...